• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

LinkedList and ConcurrentModification Exception problem

 
Branko Santo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

Thanks!


 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15448
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anupam Bhatt
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well that solves a frequently asked interview question too !
 
Branko Santo
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic