How to fix java.util.ConcurrentModificationException error when trying to pass in ArrayList

I am trying to add a new object to my ArrayList if it satisfies the condition. But I got this ConcurrentModificationExeption when I tried to run it. I hope you help me:

public void addTaskCollection(Task t){ ListIterator<Task> iterator = this.taskCollection.listIterator(); if(this.taskCollection.isEmpty()) this.taskCollection.add(t); while (iterator.hasNext()){ if(t.isOverlapped(iterator.next())) this.taskCollection.add(t); } } 

And here is the exeption error

 Exception in thread "main" java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:819) at java.util.ArrayList$Itr.next(ArrayList.java:791) at Diary.addTaskCollection(Diary.java:36) at Test.main(Test.java:50) Java Result: 1 
+1
source share
5 answers

Replace the code as follows:

 ListIterator<Task> iterator = this.taskCollection.listIterator(); boolean marker = false; if(taskCollection.isEmpty()) this.taskCollection.add(t); else { while (iterator.hasNext()) { if(iterator.next().isOverlapped(t) == false) marker = true; } } if (marker == true) taskCollection.add(t); 

to avoid a ConcurrentModificationException.

0
source

copy the array and change the original.

0
source

It seems you are faced with a race condition. Multiple threads access the same collection. Use a realistic list implementation.

In addition, you should not modify the collection (add / remove) while iterating on it using Iterator.

EDIT

ConcurrentModificationExeption sounds like a taskCollection gets access and is changed by several threads at the same time (we cannot say that the part of the code that you provide if your program is single or multi-threaded). If you share a taskCollection between multiple threads, use a list implementation with a thread.

But the error here is actually obvious due to the fact that you add an item to the collection between the moment you get an iterator on it, and as soon as you use this iterator. To fix this copy new items in the temporary list and add them all at once at the end of the iteration.

0
source

Formatted Truong answer from comments:

 ListIterator<Task> iterator = this.taskCollection.listIterator(); boolean marker = false; if(taskCollection.isEmpty()) this.taskCollection.add(t); else { while (iterator.hasNext()) { if(iterator.next().isOverlapped(t) == false) marker = true; } if (marker == true) taskCollection.add(t); } 
0
source

Support two iterators.

 import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class Example_v3 { public static void main(String[] args) { List<String> list = new ArrayList<String>(); // Insert some sample values. list.add("Value1"); list.add("Value2"); list.add("Value3"); // Get two iterators. Iterator<String> ite = list.iterator(); Iterator<String> ite2 = list.iterator(); // Point to the first object of the list and then, remove it. ite.next(); ite.remove(); /* The second iterator tries to remove the first object as well. The object does * not exist and thus, a ConcurrentModificationException is thrown. */ ite2.next(); ite2.remove(); } } 
0
source

All Articles