Java foreach 循环: for (Integer i : list) { ... }

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1818729/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 22:57:27  来源:igfitidea点击:

Java foreach loop: for (Integer i : list) { ... }

javaiteratorforeach

提问by cometta

When I use JDK5 like below

当我像下面这样使用JDK5时

ArrayList<Integer> list = new ArrayList<Integer>();  
     for (Integer i : list) { 

      //cannot check if already reached last item

   }

on the other hand if I just use an Iterator

另一方面,如果我只是使用 Iterator

ArrayList<Integer> list = new ArrayList<Integer>();
  for (Iterator i = list.iterator(); i.hasNext();) {

          //i can check whether this is last item
          if(i.hasNextItem()){
          }

    }

How can I check if I've already reached last item with for (Integer i : list) {

如何检查我是否已经到达最后一项 for (Integer i : list) {

采纳答案by Romain Linsolas

One way to do that is to use a counter:

一种方法是使用计数器:

ArrayList<Integer> list = new ArrayList<Integer>();
...
int size = list.size();
for (Integer i : list) { 
    ...
    if (--size == 0) {
        // Last item.
        ...
    }
}

Edit

编辑

Anyway, as Tom Hawtin said, it is sometimes better to use the "old" syntax when you need to get the current index information, by using a forloop or the iterator, as everything you win when using the Java5 syntax will be lost in the loop itself...

无论如何,正如 Tom Hawtin 所说,当您需要获取当前索引信息时,有时最好使用“旧”语法,通过使用for循环或iterator,因为您在使用 Java5 语法时获得的一切都将在循环中丢失本身...

for (int i = 0; i < list.size(); i++) {
    ...

    if (i == (list.size() - 1)) {
        // Last item...
    }
}

or

或者

for (Iterator it = list.iterator(); it.hasNext(); ) {
    ...

    if (!it.hasNext()) {
        // Last item...
    }
}

回答by Tom Hawtin - tackline

Sometimes it's just better to use an iterator.

有时最好使用迭代器。

(Allegedly, "85%" of the requests for an index in the posh for loop is for implementing a Stringjoin method (which you can easily do without).)

(据称,在 posh for 循环中对索引的“85%”请求是为了实现String连接方法(您可以轻松实现)。

回答by Thomas Jung

The API does not support that directly. You can use the for(int i..) loop and countthe elements or use subLists(0, size - 1)and handle the last element explicitly:

API 不直接支持。您可以使用 for(int i..) 循环并计算元素或使用subLists(0, size - 1)并显式处理最后一个元素:

  if(x.isEmpty()) return;
  int last = x.size() - 1;
  for(Integer i : x.subList(0, last)) out.println(i);
  out.println("last " + x.get(last));

This is only useful if it does not introduce redundancy. It performs betterthan the counting version (after the subList overhead is amortized). (Just in case you cared after the boxing anyway).

这仅在不引入冗余的情况下才有用。它的性能优于计数版本(在分摊 subList 开销后)。(以防万一你在意拳击之后)。

回答by Esko

Another way, you can use a pass-through object to capture the last value and then do something with it:

另一种方法是,您可以使用传递对象来捕获最后一个值,然后对其进行处理:

List<Integer> list = new ArrayList<Integer>();
Integer lastValue = null;
for (Integer i : list) {
    // do stuff
    lastValue = i;
}
// do stuff with last value