【问题标题】:Foreach loop only working in order for elements stored inside ArrayListForeach 循环仅适用于存储在 ArrayList 中的元素
【发布时间】:2017-01-20 11:18:05
【问题描述】:

我有一个产品“零食”的 ArrayList,这些产品从文件中读取并存储在此 ArrayList 中,显示它们的代码、名称、价格。 使用此代码,命名为 selectProduct() 方法:

public void selectProduct() {
 loadProducts();
    System.out.println("Insert product code");
    Scanner keyboard = new Scanner(System.in);
    for(Snack s : arraysnack){
        if(keyboard.next().equals(s.code))
            System.out.println("Product found");
        else 
            System.out.println("Product NOT found");
               break;
    }

它们的 ArrayList 是从方法“loadProducts”正确填充的,但是当我尝试提供代码输入时,如果它遵循存储的元素顺序,它会正确打印,否则它将跳过并且当我到达时 foreach 循环会自动中断最后一个产品。 例如,数组填充如下(代码,名称,价格)

PATA1   san carlo   0.4
PATA2   chipsters   0.35
KIND1    kinder 0.75
KIND2    kinder 0.5
KIND3    hippo  0.25
MARS1    mars   0.8

如果我按顺序输入代码,它会给出正确的输出(但总是在 6 次迭代后结束),但如果我跳过第一个,只有当我输入第二个时,输出才会给出“产品查找”... 如果我打印存储在 arraylist 中的 6 个代码之一,我想要一个“产品查找”输出,如果订单不被遵守,并且我想迭代超过 6 次(因为只有 6 个产品)。

【问题讨论】:

  • 找到产品后您应该break。而且您在每次迭代中都在寻找新产品。

标签: java arraylist foreach


【解决方案1】:

您可以简单地使用list.contains(),而不是按顺序遍历arraylist。

【讨论】:

    【解决方案2】:

    使用 .nextLine() 而不是 next() 以确保扫描程序通过换行符分隔输入数据。此外,您没有在 if 语句上使用括号,然后 else 后面跟着两行代码。如果您不使用方括号,则只有“else”之后的行将成为 else 块的一部分。此行之后的所有内容都将独立于 if 语句运行 - 这就是它会中断的原因。 此外,您可能每次迭代只需要一个输入。

    Scanner keyboard = new Scanner(System.in);
    String input = keyboard.nextLine();
    for(Snack s : arraysnack){
        if(input.equals(s.code)){
            System.out.println("Product found");
        } else { 
            System.out.println("Product NOT found");
            break;
        }
    }
    

    【讨论】:

      【解决方案3】:
      for(Snack s : arraysnack){
              if(keyboard.next().equals(s.code))
      

      在每次迭代中,您都从键盘请求新的输入。这是错误的!您应该在循环之前获得一次输入

      boolean isFound = false;
      String userCode = keypord.next();
      for(Snack s : arraysnack){
          if(userCode.equals(s.code)){ // <- Especially for beginners, I recommend ALWAYS use brackets!
              System.out.println("Product found");
              isFound = true;
              break; // Break here! It has been found.
          }
          //else 
              // Here you only know that it isn't the current product!
              // System.out.println("Product NOT found");
              //    break;  <- this break would have been outside if/else !!
      }
      if( !isFound ) System.out.println("Product NOT found");
      

      旁注:括号 - 使用它们!

      这个:

      if(keyboard.next().equals(s.code))
          System.out.println("Product found");
      else 
          System.out.println("Product NOT found");
          break;
      

      等价于:

      if(keyboard.next().equals(s.code)){
          System.out.println("Product found");
      }else{
          System.out.println("Product NOT found");
      }
      break;
      

      所以break 将永远发生 - 无论条件的结果是什么。

      【讨论】:

      • 知道了!现在它工作得很好......所以我只会在之前检查一个输入,然后 foreach 循环会自动检查代码是否是数组中每个 Snack 的实例?非常感谢!
      • @LudovicoMarzagalli 很高兴我能提供帮助。考虑在此处接受其中一个答案或将其投票以表扬他们的帮助。
      猜你喜欢
      • 2012-04-07
      • 1970-01-01
      • 2010-11-30
      • 1970-01-01
      • 2019-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-22
      相关资源
      最近更新 更多