当我对 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)
此外,如果有人知道反向迭代的更好习语,我很乐意尝试一下。