Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Arraylist - ConcurrentModificationException  RSS feed

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys, Kind of hit a wall with this one...
I understand the concept of why I'm getting the concurrent error with an Arraylist, however it doesn't always seem to throw the error.
Of the 2 code snippets below, only the second one throws the error. The 1st one compiles and run without any issue.
From everything I've been reading it should not allow for the removal of an item from an Arraylist inside a for each loop???

1.



2.
 
author
Marshal
Posts: 23438
138
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As mentioned in the JavaDoc, the fail-fast mechanism is best effort. This means that you can't depend on it to fail correctly... and of course, you can't depend on it to work correctly (or consistently) with all versions of the JVM, when used incorrectly, either.

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!