File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 100
Hi all. Trying to manipulate an ArrayList and i get this error, what do i do?

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19790
    
  20

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.util.ConcurrentModificationException