简体   繁体   English

线程“ AWT-EventQueue-0”中的Java,ArrayList和Exception java.util.ConcurrentModificationException

[英]Java, ArrayList and Exception in thread “AWT-EventQueue-0” java.util.ConcurrentModificationException

Im iterating through an ArrayList. 我遍历一个ArrayList。 If I use the old fashion way: 如果我使用旧的流行方式:

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. 它在ArrayList类内部的第一行失败:线程“ AWT-EventQueue-0”中的异常java.util.ConcurrentModificationException是的,我确实删除了项目-但在迭代时当然不会。 How to solve this? 如何解决呢? I dont use any threads. 我不使用任何线程。

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 ConcurrentModificationException exception. for (Baseitem item : list)循环中,您正在迭代该列表,因此对列表元素的修改将引发ConcurrentModificationException异常。

You have to use the first form of the loop if you want to modify the elements inside it. 如果要修改其中的元素,则必须使用循环的第一种形式。

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));

i is going to be 0 , 1 and 2 , but at the third iteration ( i=2 ) there will be no clients[2] , thence ConcurrentModificationException . i将是012 ,但在第三次迭代i=2将没有clients[2]从那里ConcurrentModificationException


How to avoid the problem: 如何避免该问题:

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

An important note about for-each: 有关for-each的重要说明:

The for-each loop is used with both collections and arrays. for-each循环可用于集合和数组。 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循环,因为它增加了可读性。

In your case using iterator is preferable. 在您的情况下,最好使用迭代器。

Can you also post the update() method of Baseitem ? 您还可以发布Baseitemupdate()方法吗?

You have to copy the list before for-each loop! 您必须在for-each循环之前复制列表!

List<Car> cars = new ArrayList<>(oldCars);

for(Car car : cars) {
    callDriver(car);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java:线程“ AWT-EventQueue-0”中的异常java.util.ConcurrentModificationException - Java: Exception in thread “AWT-EventQueue-0” java.util.ConcurrentModificationException Java中的线程。 “ AWT-EventQueue-0” java.util.ConcurrentModificationException - Threads in Java. “AWT-EventQueue-0” java.util.ConcurrentModificationException 线程“AWT-EventQueue-0”中的异常java.lang.ClassCastException:java.util.ArrayList - Exception in thread “AWT-EventQueue-0” java.lang.ClassCastException: java.util.ArrayList 线程“ AWT-EventQueue-0”中的异常(Java) - Exception in thread “AWT-EventQueue-0” (Java) 线程“AWT-EventQueue-0”中的java异常 - java exception in thread “AWT-EventQueue-0” 多线程ArrayList上的java.util.ConcurrentModificationException - java.util.ConcurrentModificationException on ArrayList in multi thread Java Swing GUI异常-线程“ AWT-EventQueue-0”中的异常java.util.NoSuchElementException:矢量枚举 - Java Swing GUI exception - Exception in thread “AWT-EventQueue-0” java.util.NoSuchElementException: Vector Enumeration 线程“main”java.util.ConcurrentModificationException中的异常 - Exception in thread “main” java.util.ConcurrentModificationException 线程“ AWT-EventQueue-0”中的异常java.awt.HeadlessException - Exception in thread “AWT-EventQueue-0” java.awt.HeadlessException ArrayList上的java.util.ConcurrentModificationException - java.util.ConcurrentModificationException on ArrayList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM