线程“AWT-EventQueue-0”中的 Java、ArrayList 和异常 java.util.ConcurrentModificationException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17067626/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:52:59  来源:igfitidea点击:

Java, ArrayList and Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException

java

提问by John Smith

Im iterating through an ArrayList. If I use the old fashion way:

我正在遍历一个 ArrayList。如果我使用旧时尚的方式:

for (int i = 0; i < list.size(); i++)
{
    list.get(i).update();;
}

it runs ok. But with this:

它运行正常。但是有了这个:

for (Baseitem item : list)
{
    item.update();
}

it fails at the first line, inside ArrayList class: Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException yes, outside I do remove items - but certainly not while iterating. How to solve this? I dont use any threads.

它在第一行失败,在 ArrayList 类中:线程“AWT-EventQueue-0”中的异常 java.util.ConcurrentModificationException 是的,在外面我确实删除了项目 - 但肯定不是在迭代时。如何解决这个问题?我不使用任何线程。

回答by eternay

You should avoid modifying elements in a list while iterating that list.

您应该避免在迭代列表时修改列表中的元素。

With the for (int i...)loop, you are not iterating the list, so you can modify the elements inside.

使用for (int i...)循环,您不会迭代列表,因此您可以修改其中的元素。

In the for (Baseitem item : list)loop, you are iterating the list, so the modification of the elements of the list will raise the ConcurrentModificationExceptionexception.

for (Baseitem item : list)循环中,您正在迭代列表,因此对列表元素的修改将引发ConcurrentModificationException异常。

You have to use the first form of the loop if you want to modify the elements inside it.

如果要修改其中的元素,则必须使用循环的第一种形式。

回答by António Almeida

This happened to me because I was iterating through a list for removing all the items, with a for loop.

这发生在我身上,因为我正在遍历一个列表以删除所有项目,并使用 for 循环。

Example clients: [1, 2, 3]

示例clients[1, 2, 3]

for(int i=0 ; i<clients.size() /* 3 */ ; i++)
    clients.remove(clients.get(i));

iis going to be 0, 1and 2, but at the third iteration (i=2)there will be no clients[2], thence ConcurrentModificationException.

i将是01并且2,但是在第三次迭代( i=2)时将没有clients[2],然后ConcurrentModificationException



How to avoid the problem:

如何避免问题:

while(clients.size() > 0)
    clients.remove(0);

回答by Mubin

An important note about for-each:

关于 for-each 的重要说明:

The for-each loop is used with both collections and arrays. It's intended to simplify the most common form of iteration, where the iterator or index is used solely for iteration, and not for any other kind of operation, such as removing or editing an item in the collection or array. When there is a choice, the for-each loop should be preferred over the for loop, since it increases legibility.

for-each 循环用于集合和数组。它旨在简化最常见的迭代形式,其中迭代器或索引仅用于迭代,而不用于任何其他类型的操作,例如删除或编辑集合或数组中的项目。当有选择时,for-each 循环应该优先于 for 循环,因为它增加了易读性。

In your case using iterator is preferable.

在您的情况下,最好使用迭代器。

Can you also post the update()method of Baseitem?

也可以发一下update()方法Baseitem吗?