File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

doubt in John Meyer's mock exam - ConcurrentModificationException!

 
Aakash Goel
Ranch Hand
Posts: 198
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


prints 2 followed by ConcurrentModificationException

while



compiles and runs fine.

I feel that in both the places modifications are being done on the original queue/array. So, why exception in first code and not in second?
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The "enhanced for" loop, in the first case, uses an iterator to iterate through the collection -- this is actually hidden, but it is there nonetheless.

During iteration of a priority queue (and actually with most of the collections), you are not allowed to modify the structure of the collection, except via the iterator methods. A concurrent modification exception gets thrown when this is detected.

Henry
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic