aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in John Meyer's mock exam - ConcurrentModificationException! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in John Meyer Watch "doubt in John Meyer New topic
Author

doubt in John Meyer's mock exam - ConcurrentModificationException!

Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198


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?

SCJP 5 95%
SCJP FAQ | SCJP Mock Tests | SCJP Tipline | Generics
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18997
    
  40


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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: doubt in John Meyer's mock exam - ConcurrentModificationException!