Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

doubt in John Meyer's mock exam - ConcurrentModificationException!  RSS feed

 
Aakash Goel
Ranch Hand
Posts: 198
  • 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
Sheriff
Posts: 22846
119
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!