This week's book giveaway is in the JavaScript forum.
We're giving away four copies of JavaScript Promises Essentials and have Rami Sarieddine on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Exception using ListIterator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exception using ListIterator" Watch "Exception using ListIterator" New topic
Author

Exception using ListIterator

Sujoy Mukherjee
Ranch Hand

Joined: Dec 31, 2005
Posts: 51

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.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4457
    
    8

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

Joined: Dec 31, 2005
Posts: 51

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

Joined: Apr 06, 2010
Posts: 4457
    
    8

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

Joined: Dec 31, 2005
Posts: 51

Thanks again.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39755
    
  28
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).
jatan bhavsar
Ranch Hand

Joined: Jul 23, 2008
Posts: 296

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
Sheriff

Joined: Oct 13, 2005
Posts: 39755
    
  28
No.
Sujoy Mukherjee
Ranch Hand

Joined: Dec 31, 2005
Posts: 51

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?
changu mani
Ranch Hand

Joined: Aug 31, 2012
Posts: 30
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
Sheriff

Joined: Oct 13, 2005
Posts: 39755
    
  28
Changu Mani, welcome to the Ranch
Sujoy Mukherjee
Ranch Hand

Joined: Dec 31, 2005
Posts: 51

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
Sheriff

Joined: Oct 13, 2005
Posts: 39755
    
  28

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception using ListIterator