• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Exception using ListIterator

 
Ranch Hand
Posts: 51
Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear friends,
I have a written a program using LinkedList and ListIterator.
The code looks like this:


But when i am running this code using netbeans 6.9, i am getting this following Exception:


I don't know why? All I know probably line no 27( removeLast() method) causes the Exception. Honestly i don't have that much idea about Iterators or how they are stored internally in memory.
I did a quick search and what i found(or it seems to me) that iterator is like cursors.....and it points in between elements.
What i don't understand is why the exception is coming?
also if I comment out at line 27 and uncomment line no 29, I would not get the error but in that case the last while loop will not work. Why?

Please help.
Thanks in advance.
 
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at the documentation for ConcurrentModificationException, and it explains that they usually occur if you try to modify a collection while iterating over it. In particular, it says:

Note that this exception does not always indicate that an object has been concurrently modified by a different thread. If a single thread issues a sequence of method invocations that violates the contract of an object, the object may throw this exception. For example, if a thread modifies a collection directly while it is iterating over the collection with a fail-fast iterator, the iterator will throw this exception.


The bit in bold describes what you're doing. On line 31 you're using the same ListIterator as before (which seems slightly pointless, because you've already got to the end, but anyway...). The ListIterator detects that you've modified the collection (line 27) since it was created, and throws the exception.

If you need to update the List while iterating, you can use the modifying methods of the ListIterator, as those are safe to use.
 
Sujoy Mukherjee
Ranch Hand
Posts: 51
Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot.
If I have understood properly, line no 27 is modifying the ListIterator collection.....that means after line 27 the underlying Iterator is not stable anymore.
that is why even if I change line no 31 from to , even though its not working and still throwing exception.
Am I correct?
One more thing can you tell me something more about how internally Iterators store the elements and how they work?
And from your text ....what is Fail fast Iterator?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know how they are stored internally. You could check the source code if you want (comes with the JDK distribution), but you don't need to know to understand the behaviour.

This is an example of a fail-fast iterator, and it means that if the iterator may not be stable any more it immediately throws an exception. The alternative would be that it would continue to work, but might behave in unpredictable ways. That would lead to very hard to find bugs, which is why they are made fail-fast.
 
Sujoy Mukherjee
Ranch Hand
Posts: 51
Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Scope. That’t what you need. Iterators should be given the smallest scope possible, always as local variables. Your Iterator is still in scope when you get to the removeLast() invocation, so restrict its scope with an additional pair of{}. You will of course need a new Iterator for the second loop.
It would have been better to use a for‑each loop (= enhanced for loop).
 
Ranch Hand
Posts: 300
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I dont know is it right question to ask or not but can we utilize synchonization here to avoid concurrent modification without changing his code?

Regards
Jatan
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No.
 
Sujoy Mukherjee
Ranch Hand
Posts: 51
Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Scope. That’t what you need. Iterators should be given the smallest scope possible, always as local variables. Your Iterator is still in scope when you get to the removeLast() invocation, so restrict its scope with an additional pair of{}. You will of course need a new Iterator for the second loop.
It would have been better to use a for‑each loop (= enhanced for loop).



Thanks Campbell.

so restrict its scope with an additional pair of{}


Could you please elaborate on this?
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

What i don't understand is why the exception is coming?
also if I comment out at line 27 and uncomment line no 29, I would not get the error but in that case the last while loop will not work. Why?



The ListIterator has reached the end so it is not printing. Make it hasPrevious and it prints.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Changu Mani, welcome to the Ranch
 
Sujoy Mukherjee
Ranch Hand
Posts: 51
Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The ListIterator has reached the end so it is not printing. Make it hasPrevious and it prints.



Changu i have already done that. perhaps you did not go through one of my previous posts:

If I have understood properly, line no 27 is modifying the ListIterator collection.....that means after line 27 the underlying Iterator is not stable anymore.
that is why even if I change line no 31 from
view plaincopy to clipboardprint?
lit.hasnext()
to
view plaincopy to clipboardprint?
lit.hasPrevious()
, even though its not working and still throwing exception.
Am I correct?



Anyway but that was solved. my last question was based on campbell's last comment as I already mentioned.
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic