• Post Reply Bookmark Topic Watch Topic
  • New Topic

ConcurrentModificationException when running this program  RSS feed

 
Jacob Sonia
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to run the code against a path and add only files to it. I know why i am getting concurrentModificationException, because i am trying to act on the same list on which i am removing things. I can get this code working by creating another list and doing the check if File.isfile and adding only those things to the new list, but was thinking if there is any other optimized way to do it. Please suggest

 
Christian Dillinger
Ranch Hand
Posts: 200
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to use an Iterator (let's name it iter) and call iter.remove(). If you iterator a list via "for(Foo x :xList)" you aren't allowed to call xList.remove().

If you do it like this, it's okay...

 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The same can also be written as a for-statement:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!