File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes LinkedList Question 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 "LinkedList Question" Watch "LinkedList Question" New topic

LinkedList Question

Akhil Maharaj
Ranch Hand

Joined: Sep 29, 2007
Posts: 63
Hi ,

Can anybody explain why in the output I am not getting Italy. All I am seeing in the output is hello world.

Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
Because after the second pass through the loop i is 2, while q.size() is 1 since the first two elements have been removed. Modifying a collection that you're iterating through is a dangerous thing to do. If you use an Iterator instead you'll see all elements:

or you can always print the first element in a loop:

I agree. Here's the link:
subject: LinkedList Question
It's not a secret anymore!