Forums Register Login

current modification exception

+Pie Number of slices to send: Send


this gave me


Exception in thread "main" java.util.ConcurrentModificationException
at java.util.LinkedList$ListItr.checkForComodification(Unknown Source)
at java.util.LinkedList$ListItr.next(Unknown Source)
at MyList.getNext(MyList.java:23)
at MyList.main(MyList.java:49)



i don't understand why.

apparently the line

is the problem.
but i'm not modifying my linked list with this code.
+Pie Number of slices to send: Send
When your iterator calls the next method, it checks to see if its expectedModCount is equal to the list's modCount it is iterating over. If these numbers don't match, the iterator thinks that the list has been modified and it throws the exception that you received.
+Pie Number of slices to send: Send
To add to what Daryl already said, the reason these numbers don't match, is because you're initializing i with an iterator and you add values to the list before using it.

It's usually a bad idea to keep references to iterators outside of a method scope. The only time I've needed this myself was when I was implementing an iterator that traverses trees depth-first, and kept a reference to a recursive iterator.
+Pie Number of slices to send: Send
The LinkedList class already has a built in iterator anyway, so why would you want to make a new one?
If you want to go through the elements in your MyList class, you might want to look into making it iterable by implementing the Iterable interface.
Won't you please? Please won't you be my neighbor? - Fred Rogers. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1269 times.
Similar Threads
Exception using ListIterator
unmodifiableList(List list)
ConcurrrentModificationErro
add() method of ListIterator, don't know what I did wrong?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 05:41:27.