Line 1 uses Enhanced for loop, alternative for Iterator, implicitly iterators are used, hence the above for loop line can be substituted for
Now the first iteration of for loop will execute smoothly and apple will be added, but as implicit iterator excutes and performs s = it.next() , Concurrent Modification Run Time Exception is Thrown Hence if Iterator used, addition an deletion should always be done through Iterator
Joined: May 09, 2007
Originally posted by Prasad Maddipatla:
for(String s : pq)
this is the line which is causing the runtime exception.
here you are getting ConcurrentModificationException That is because you are concurrently iterating a Collection and modifying the same collection. here is the excerpt taken from J2SE 5.0 API,
This exception may be thrown by methods that have detected concurrent modification of an object when such modification is not permissible.
For example, it is not generally permissible for one thread to modify a Collection while another thread is iterating over it. In general, the results of the iteration are undefined under these circumstances.