简体   繁体   English

我需要使用for循环的方法的帮助

[英]I need help with a method using a for-loop

  public void collect( int ordNum )
  {
       Basket b = new Basket(ordNum);
       for (Basket b : conveyerBelt) { // line 4
           readyCollected.add(b);
       }
  }

What I'm trying to do with this method is search through an ArrayList for a orderNum. 我正在尝试使用此方法的方法是在ArrayList中搜索orderNum。 When I find it, I want to add the orderNum i entered in to the readyCollected. 找到它后,我想将输入的orderNum添加到readyCollected中。 Problem: I get an error message on line 4. 问题:我在第4行收到一条错误消息。

Was wondering if you can guys can help me implement this method. 想知道你们是否可以帮助我实现此方法。

BTW, the conveyerBelt already contains Basket with an order number. 顺便说一句,输送带已包含带有订单号的购物篮。 So lets say the conveyerBelt contains Basket with order number 3, and I enter in 3, I want it to add Basket to Arraylist readyCollected containing order number 3. 因此,可以说,输送带包含订单号为3的购物篮,我输入3,我希望它向包含订单号3的Arraylist readyCollected添加购物篮。

I'm guessing your error is that conveyerBelt is not Iterable. 我猜你的错误是输送器皮带不是可迭代的。 To use the foreach construct, you must make sure the object you are Iterating over implements Iterable. 要使用foreach构造,必须确保要迭代的对象实现Iterable。

http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.14.2 http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.14.2

Excerpt: 摘抄:

The Expression must either have type Iterable or else it must be of an array type (§10.1), or a compile-time error occurs. 表达式必须具有Iterable类型,否则必须具有数组类型(第10.1节),否则会发生编译时错误。

The scope of a local variable declared in the FormalParameter part of an enhanced for statement (§14.14) is the contained Statement 在增强的for语句(第14.14节)的FormalParameter部分中声明的局部变量的范围是所包含的Statement

The meaning of the enhanced for statement is given by translation into a basic for statement. 增强的for语句的含义是通过翻译成基本的for语句来给出的。

If the type of Expression is a subtype of Iterable , then let I be the type of the expression Expression.iterator() . 如果Expression的类型是Iterable的子Iterable ,那么让我成为Expression.iterator()的类型。 The enhanced for statement is equivalent to a basic for statement of the form: 增强的for语句等效于以下形式的基本for语句:

for (I #i = Expression.iterator(); #i.hasNext(); ) {

        VariableModifiersopt Type Identifier = #i.next();
   Statement
}

You don't give nearly enough information to solve this properly. 您没有提供足够的信息来正确解决此问题。

  • What is the error? 有什么错误?
  • What types are conveyerBelt and readyCollected? 传送带和readyCollected是什么类型的?

Also what do you mean by "order number" - the Nth item in the list or is it some field? 另外,“订单号”是什么意思-列表中的第N个项目还是某个字段?

conveyerBelt应被声明为Collection<Basket >通过Collection我的意思是任何Collection

Is it a compile error? 它是编译错误吗? Maybe something about a symbol and conveyerBelt ? 也许关于符号和conveyerBelt一些事情? Because it's not defined in the snippet you've given us. 因为您没有在代码段中定义它。

You are using the same name ( b ) for two variables. 您正在为两个变量使用相同的名称( b )。 That's probably the cause of the error you get, but I am not sure what you are trying to achieve with that code. 这可能是导致您出错的原因,但是我不确定您尝试使用该代码实现什么。

I think it's something like 我觉得有点像

public void collect( int ordNum )
{
    for (Basket b : conveyerBelt) {
        if (b.getOrdNum() == ordNum) {
            readyCollected.add(b);
            return;  // if only one occurrence wanted
        }
    }
}

assuming conveyerBelt is some Collection of Basket and readyCollected is some List of Basket . 假设conveyerBelt是一些收集BasketreadyCollected是一些名单Basket

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM