This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Java in General and the fly likes LinkedList and ConcurrentModification Exception problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "LinkedList and ConcurrentModification Exception problem" Watch "LinkedList and ConcurrentModification Exception problem" New topic

LinkedList and ConcurrentModification Exception problem

Branko Santo
Ranch Hand

Joined: Oct 15, 2005
Posts: 72
I am sorry to post such lengthy code but I just can't get it to work.
I load up a LinkedList form disk and sort it and try to access the objects inside, but it throws the Exception. Really bummed I don't know what to do.
I have bolded the trace to exception, any help would be great!


Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15037

Do you understand what ConcurrentModificationException means? Look up the API documentation for it.

You can't iterate over a List and modify the list at the same time. Instead of this:

Try this, call remove() on the iterator instead of on the collection:

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24195

Don't use sezona.remove(game). Use iterator.remove() . Not only will the exception go away, but the performance will be much better for long lists. You can't call add() or remove() on a Collection while you're iterating over it, or the Iterator will throw an exception, as you found out.

[Jess in Action][AskingGoodQuestions]
Anupam Bhatt
Ranch Hand

Joined: Mar 12, 2004
Posts: 81
Well that solves a frequently asked interview question too !
Branko Santo
Ranch Hand

Joined: Oct 15, 2005
Posts: 72
Thanks people!!!

I know what the exception did, but the problem is I saw no problem with the code I already had something like that, but it must be that last time I was using something like you did.

Anyways really thanks!!!

A million
I agree. Here's the link:
subject: LinkedList and ConcurrentModification Exception problem
It's not a secret anymore!