对于java中的每个循环,Iterator等效的是什么?

时间:2018-06-19 11:05:57

标签: java for-loop foreach

有人知道Iterator的等价物吗?

for (Type a : list) {
    for (Type b : a) {

    }
}

我的方法是

for (Iterator<Type> it1 = a.iterator(); it1.hasNext();){
  for (Iterator<Type> it2 = b(a).iterator(); it2.hasNext();) {

  }
}

但它没有用。

5 个答案:

答案 0 :(得分:1)

You missing missing calls to Iterator's next() method, which are required to advance the iterators and obtain the current value.

for (Iterator<Type> it1 = list.iterator(); it1.hasNext();) {
  Type a = it1.next();
  for (Iterator<Type> it2 = a.iterator(); it2.hasNext();) {
      Type b = it2.next();
  }
}

答案 1 :(得分:1)

It's right there in the JLS

The enhanced for statement is equivalent to a basic for statement of the form:

for (I #i = Expression.iterator(); #i.hasNext(); ) {
    {VariableModifier} TargetType Identifier = (TargetType) #i.next();
    Statement
}

...and then shows this example:

List<? extends Integer> l = ...
for (float i : l) ...

will be translated to:

for (Iterator<Integer> #i = l.iterator(); #i.hasNext(); ) {
    float #i0 = (Integer)#i.next();
    ...

So your version is mostly fine, except:

  1. You're trying to call a.iterator to start, but your enhanced for example shows a as the outer loop variable; it should be list to start with.
  2. You need to call next
  3. You need to use the outer loop variable (a) when getting the inner loop iterator

(You don't need the casts that they give in the example; they needed them for completeness, but you're just using Type where they were using float to loop through an Integer list.)

So:

for (Iterator<Type> it1 = list.iterator(); it1.hasNext();) {
// -----------------------^^^^
  Type a = it1.next();   // <==
  for (Iterator<Type> it2 = a.iterator(); it2.hasNext();) {
// -------------------------^
    Type b = it2.next(); // <==
  }
}

答案 2 :(得分:1)

for(Type a : list){
  for(Type b : a){
    // Do something with b
  }
}

to

for(Iterator<Type> itList = list.iterator(); itList.hasNext();){
  Type a = itList.next();
  for (Iterator<Type> itA = a.iterator(); itA.hasNext();){
    Type b = itA.next();
    // Do something with b
  }
}

.next() is used to get the value in the iterator-iteration. So in your initial code the b(a) should have been it2.next() (and the a and b should have been list and a).

Also, Type should be Iterable itself, otherwise the initial nested for-leach loops wouldn't have worked to begin with. The following would have made more sense if Type is just a DTO-object:

for(List<Type> a : list){  // `list` is of type List<List<Type>> in this case
  for(Type b : a){
    // Do something with b
  }
}

In which case the Iterator<Type> itList should have been Iterator<List<Type>> itList as well.

答案 3 :(得分:0)

Iterator<Type> itA = list.iterator();

while(itA.hasNext()) {
    Iterator<Type> itB = itA.next().iterator();

    while(itB.hasNext()) {
        Type b = itB.next();
        // TODO payload
    }
}

答案 4 :(得分:-1)

for (Iterator<List<Type>> it1 = a.iterator(); it1.hasNext();) {
            for (Iterator<Type> it2 = it1.next().iterator(); it2.hasNext();)
               Type b=it2.next();

        }