This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in John Meyer Watch "doubt in John Meyer New topic

doubt in John Meyer's mock exam - ConcurrentModificationException!

Aakash Goel
Ranch Hand

Joined: May 26, 2008
Posts: 198

prints 2 followed by ConcurrentModificationException


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

Joined: Sep 28, 2004
Posts: 20535

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.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I agree. Here's the link:
subject: doubt in John Meyer's mock exam - ConcurrentModificationException!
It's not a secret anymore!