When i compile this example i get the following error:
Exception in thread "main" java.util.ConcurentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java 372)
at java.util.AbstractList$Itr.next(AbstractList.java 343)
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
question Can you explain what i am doing wrong in understanding or coding?
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.
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
Joined: Sep 05, 2009
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
- Iterator as being the "pointer" to an element of that same ARRAY
-> [array_name||element1][[element2][....][[elementN] Iterator................^
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