通过 ArrayList 进行反向迭代会出现 IndexOutOfBoundsException

发布于 2024-07-13 21:52:53 字数 959 浏览 8 评论 0原文

当我反向迭代 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

温馨耳语 2024-07-20 21:52:53

完全避免索引吗? 怎么样:

for (ListIterator iterator = list.listIterator(list.size()); iterator.hasPrevious();) {
  final Object listElement = iterator.previous();
}

Avoid indexes altogether? How about:

for (ListIterator iterator = list.listIterator(list.size()); iterator.hasPrevious();) {
  final Object listElement = iterator.previous();
}
我很坚强 2024-07-20 21:52:53

list.size() - 1 开始迭代,因为数组(或 ArrayList)元素的编号从 0 到 1,小于列表的大小。 这是一个相当标准的习惯用法:

for (int j = list.size() - 1; j >= 0; j--) {
    // whatever
}

请注意,您的前向迭代之所以有效,是因为它在到达 list.size() 之前停止。

Start the iteration at list.size() - 1 because array (or ArrayList) elements are numbered from 0 up through 1 less than the size of the list. This is a fairly standard idiom:

for (int j = list.size() - 1; j >= 0; j--) {
    // whatever
}

Note that your forward iteration works because it stops before reaching list.size().

小巷里的女流氓 2024-07-20 21:52:53

我知道这是一个老问题,但 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?

尹雨沫 2024-07-20 21:52:53

最优雅的方法是反转数组,然后使用直接(甚至隐式)迭代器:

Collections.reverse(arrayList);
for (Object item : arrayList) {
    ...
}

The most elegant way is to reverse the array and then use a direct (or even implicit) iterator :

Collections.reverse(arrayList);
for (Object item : arrayList) {
    ...
}
只有影子陪我不离不弃 2024-07-20 21:52:53

list.size() 超出了最后一个允许的索引。

for(int j = list.size() - 1; j >= 0; j--) {
  System.out.println(list.get(j));
}

The list.size() is past the last allowable index.

for(int j = list.size() - 1; j >= 0; j--) {
  System.out.println(list.get(j));
}
花想c 2024-07-20 21:52:53

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.

微凉 2024-07-20 21:52:53

如果列表相当小,因此性能不是真正的问题,则可以使用 Google GuavaLists 类的 reverse 方法>。 产生漂亮的 for-each 代码,并且原始列表保持不变。 此外,反向列表由原始列表支持,因此对原始列表的任何更改都将反映在反向列表中。

import com.google.common.collect.Lists;

[...]

final List<String> myList = Lists.newArrayList("one", "two", "three");
final List<String> myReverseList = Lists.reverse(myList);

System.out.println(myList);
System.out.println(myReverseList);

myList.add("four");

System.out.println(myList);
System.out.println(myReverseList);

产生以下结果:

[one, two, three]
[three, two, one]
[one, two, three, four]
[four, three, two, one]

这意味着 myList 的反向迭代可以写为:

for (final String someString : Lists.reverse(myList) {
    //do something
}

If the lists are fairly small so that performance is not a real issue, one can use the reverse-metod of the Lists-class in Google Guava. Yields pretty for-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.

import com.google.common.collect.Lists;

[...]

final List<String> myList = Lists.newArrayList("one", "two", "three");
final List<String> myReverseList = Lists.reverse(myList);

System.out.println(myList);
System.out.println(myReverseList);

myList.add("four");

System.out.println(myList);
System.out.println(myReverseList);

Yields the following result:

[one, two, three]
[three, two, one]
[one, two, three, four]
[four, three, two, one]

Which means that reverse iteration of myList can be written as:

for (final String someString : Lists.reverse(myList) {
    //do something
}
堇年纸鸢 2024-07-20 21:52:53

如果您熟悉 foreach 循环,则可以执行此操作。

List<String> list = new ArrayList<String>();
list.add("ABC");
list.add("DEF");
list.add("GHI");

ListIterator<String> listIterator = list.listIterator(list.size());

while(listIterator.hasPrevious()){
  System.out.println(listIterator.previous());
}

You can do this if you are comfortable with foreach loop.

List<String> list = new ArrayList<String>();
list.add("ABC");
list.add("DEF");
list.add("GHI");

ListIterator<String> listIterator = list.listIterator(list.size());

while(listIterator.hasPrevious()){
  System.out.println(listIterator.previous());
}
ぶ宁プ宁ぶ 2024-07-20 21:52:53

您可以通过一行来反转

Collections.reverse(list);

ArrayList arrayList = new ArrayList();

arrayList.add("A");
arrayList.add("B");

System.out.println("Before Reverse Order : " + arrayList);

Collections.reverse(arrayList);

System.out.println("After Reverse : " + arrayList);

Output

Before Reverse Order : [A, B]
After Reverse : [B, A]

You can reverse by one line that is

Collections.reverse(list);

ArrayList arrayList = new ArrayList();

arrayList.add("A");
arrayList.add("B");

System.out.println("Before Reverse Order : " + arrayList);

Collections.reverse(arrayList);

System.out.println("After Reverse : " + arrayList);

Output

Before Reverse Order : [A, B]
After Reverse : [B, A]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文