Idiomatic way to use for-each loop given an iterator?

后端 未结 9 1626
星月不相逢
星月不相逢 2020-12-03 09:52

When the enhanced for loop (foreach loop) was added to Java, it was made to work with a target of either an array or Iterable.

for ( T item : /*         


        
9条回答
  •  旧时难觅i
    2020-12-03 10:32

    The Apache Commons Collections API has a class called IteratorIterable to do exactly this:

    Iterator iter;
    for (X item : new IteratorIterable(iter)) {
        ...
    }
    

提交回复
热议问题