Just to expand on this a bit more in case you're still unsure of the documentation at ArrayList (Java Platform SE 7 )
, there are two
possible causes of ConcurrentModificationException
- "If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally [...] (A structural modification is any operation that adds or deletes one or more elements, or explicitly resizes the backing array; merely setting the value of an element is not a structural modification.)"
- "If the list is structurally modified at any time after the iterator is created, in any way except through the iterator's own remove or add methods", i.e., the "fail-fast behaviour"
#1 only occurs if you have 2 or more threads
using (and structurally modifying) the ArrayList at the same time. The use of "List list = Collections.synchronizedList(new ArrayList(...));
" is applicable for this scenario. (If you're not sure what I mean by "thread", see Lesson: Concurrency
and Processes and Threads
#2 occurs even when you have just one thread
using the ArrayList. The word "concurrent" in the exception in this case does not
refer to concurrent access of the ArrayList by multiple threads, but to performing structural modification after having created the iterator for the ArrayList. Collections.synchronizedList()
help in this scenario.
In your case, it is #2, as the good members above have helpfully posted.