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)
at ListExample.main(ListExample.java:18)
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?
Thansk for your hint,tips and tricks.
Ronald
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35232
7
posted
0
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.
Initialise the iterator after adding the elements to list and not before you have added the elements to list.
Regards,
Minakshi
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35232
7
posted
0
Oh boy, 4 people in total agreement!
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32643
4
posted
0
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
posted
0
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] 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 !
p.s
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