aspose file tools*
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
Author

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(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: 42601
    
  65
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.


Ping & DNS - my free Android networking tools app
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


ConcurrentModificationException

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


Bye,
Nicola
Monika Joshi
Greenhorn

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.

Regards,
Minakshi
Ulf Dittmer
Marshal

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

Joined: Oct 13, 2005
Posts: 39814
    
  28
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]
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: List Iterator error - please explain