通过 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)
此外,如果有人知道反向迭代的更好习惯用法,我会很乐意尝试。
When I reverse iterate over an ArrayList I am getting a IndexOutOfBoundsException. I tried doing forward iteration and there is no problem. I expect and know that there are five elements in the list. The code is below:
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));
}
Forward iteration - which is working fine, but not useful for me:
for(int j=0;j<list.size();j++){
System.out.println(list.isEmpty());
System.out.println(list.get(j));
} // this worked fine
The error:
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)
Also if anyone knows of a better idiom for reverse iteration I would be happy to try that out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
完全避免索引吗? 怎么样:
Avoid indexes altogether? How about:
从
list.size() - 1
开始迭代,因为数组(或ArrayList
)元素的编号从 0 到 1,小于列表的大小。 这是一个相当标准的习惯用法:请注意,您的前向迭代之所以有效,是因为它在到达
list.size()
之前停止。Start the iteration at
list.size() - 1
because array (orArrayList
) elements are numbered from 0 up through 1 less than the size of the list. This is a fairly standard idiom:Note that your forward iteration works because it stops before reaching
list.size()
.我知道这是一个老问题,但 Java 包含一个
Collections.reverse( List)
方法。 为什么不直接反转它并进行前向迭代呢?I know this is an old question, but Java contains a
Collections.reverse( List<T> )
method. Why wouldn't you just reverse it and do forward iteration?最优雅的方法是反转数组,然后使用直接(甚至隐式)迭代器:
The most elegant way is to reverse the array and then use a direct (or even implicit) iterator :
list.size() 超出了最后一个允许的索引。
The list.size() is past the last allowable index.
Java 数组是零索引的。
您必须设置 j = list.size() - 1 并继续直到 j = 0。
Java arrays are zero-indexed.
You will have to set j = list.size() - 1 and continue until j = 0.
如果列表相当小,因此性能不是真正的问题,则可以使用
Google Guava
Lists 类的reverse
方法>。 产生漂亮的for-each
代码,并且原始列表保持不变。 此外,反向列表由原始列表支持,因此对原始列表的任何更改都将反映在反向列表中。产生以下结果:
这意味着 myList 的反向迭代可以写为:
If the lists are fairly small so that performance is not a real issue, one can use the
reverse
-metod of theLists
-class inGoogle Guava
. Yields prettyfor-each
-code, and the original list stays the same. Also, the reversed list is backed by the original list, so any change to the original list will be reflected in the reversed one.Yields the following result:
Which means that reverse iteration of myList can be written as:
如果您熟悉 foreach 循环,则可以执行此操作。
You can do this if you are comfortable with foreach loop.
您可以通过一行来反转
Collections.reverse(list);
Output
You can reverse by one line that is
Collections.reverse(list);
Output