File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes List Iterator error - please explain Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "List Iterator error - please explain" Watch "List Iterator error - please explain" New topic

List Iterator error - please explain

Ronald Vermeij
Ranch Hand

Joined: Sep 05, 2009
Posts: 37

When i compile this example i get the following error:
Exception in thread "main" java.util.ConcurentModificationException
at java.util.AbstractList$Itr.checkForComodification( 372)
at java.util.AbstractList$ 343)
at ListExample.main(

I want to print all the single contents of the list, controlled, element by element,
I want to do in in a different way then
Can you explain what i am doing wrong in understanding or coding?

Thansk for your hint,tips and tricks.

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
That's because the list has been changed after you obtained the iterator. If you get the iterator after adding all the elements to the list (in other words, if you move line 6 after line 15), then it should work just fine.
Amit Vinod Dali
Ranch Hand

Joined: Apr 14, 2010
Posts: 42
Just define the "Iterator" after adding elements and before using it to the list e.g.
just before


Nicola Garofalo
Ranch Hand

Joined: Apr 10, 2010
Posts: 308
At line 6 you get an Iterator from an empty list, and then you begin filling the list with content.

Move line 6 to line 16.

And please have a look at the Runtime Exception ConcurrentModificationException

Monika Joshi

Joined: Apr 19, 2010
Posts: 9
Initialise the iterator after adding the elements to list and not before you have added the elements to list.

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Oh boy, 4 people in total agreement!
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46367
Ulf Dittmer wrote:Oh boy, 4 people in total agreement!
It would be even more remarkable if rob and I had posted and agreed with each other
Ronald Vermeij
Ranch Hand

Joined: Sep 05, 2009
Posts: 37
Thanks Ulf, Amit, Nicola && Minakshi for the explanation. I see where it went wrong!
1. In my mind i imagined a picture of a
- ArrayList as being literally an ARRAY of elements, things, stuff
-> [array_name||element1][[element2][....][[elementN]

- Iterator as being the "pointer" to an element of that same ARRAY
-> [array_name||element1][[element2][....][[elementN]

Thanks to your explanation I now understand that:
- An Iterator - only should be created, getted AFTER a Collection-type, List,Set, has been filled with [elements].
- An Iterator - 'works the same as an "ArrayIndex pointer from C", walking from the Head to the Tail of the array.

Thanks everyone !

This "mind-mixup" comes from also having knowledge in other languages like Pascal, C, C++.
That is what makes me stumble most of the times... old thought and design patterns :-)
Time for a Brain.flush() operation on old language skills before adding more Java to it
I agree. Here's the link:
subject: List Iterator error - please explain
It's not a secret anymore!