Java foreach loop: for (Integer i : list) { ... }
当我使用JDK5时,如下所示
ArrayList<Integer> list = new ArrayList<Integer>();
for (Integer i : list) {
//cannot check if already reached last item
}
另一方面,如果我只是使用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()){
}
}
如何检查我是否已经到达最后一项for (Integer i : list) {