通过 ArrayList 的反向迭代给出了 IndexOutOfBoundsException
当我反向迭代 ArrayList 时,我得到的是 IndexOutOfBoundsException。我尝试进行向前迭代,没有问题。我期望并知道列表中有五个要素。代码如下:
Collection rtns = absRtnMap.values();
List list = new ArrayList(rtns);
Collections.sort(list);
for(int j=list.size();j>0;j=j-1){
System.out.println(list.get(j));
}
前向迭代 - 工作正常,但对我没用:
for(int j=0;j<list.size();j++){
System.out.println(list.isEmpty());
System.out.println(list.get(j));
} // this worked fine
错误:
Exception in thread "Timer-0" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at model.Return.getReturnMap(Return.java:61)
at controller.Poller$1.run(Poller.java:29)
at java.util.TimerThread.mainLoop(Unknown Source)
at java.util.TimerThread.run(Unknown Source)
此外,如果有人知道反向迭代的更好成语,我很乐意尝试一下。