Two Laptop Bag*
The moose likes Beginning Java and the fly likes java.util.ConcurrentModificationException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "java.util.ConcurrentModificationException" Watch "java.util.ConcurrentModificationException" New topic
Author

java.util.ConcurrentModificationException

Rob Brew
Ranch Hand

Joined: Jun 23, 2011
Posts: 99
Hi all. Trying to manipulate an ArrayList and i get this error, what do i do?

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

When you iterate over most collections (either using an explicit Iterator or by using the for-each loop), any method call that modifies the collection will throw this exception. There are only two solutions:
1) use an explicit iterator and use the iterator's remove() method.
2) for lists only, loop using indexing. Be aware that when you delete an element, the indexing will also change. It's usually safest to loop backwards:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: java.util.ConcurrentModificationException
 
Similar Threads
JAVA BATTLESHIP HELP ! PLEASE
Array overwritten
Anonymous inner class
I am new to Java programming
Calling seperate functions