Win a copy of High Performance Python for Data Analytics this week in the Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

List Iterator error - please explain

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just define the "Iterator" after adding elements and before using it to the list e.g.
just before


ConcurrentModificationException

 
Ranch Hand
Posts: 308
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Initialise the iterator after adding the elements to list and not before you have added the elements to list.

Regards,
Minakshi
 
Ulf Dittmer
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh boy, 4 people in total agreement!
 
Marshal
Posts: 71762
312
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic