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

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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: 39547
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:

Ping & DNS - updated with new look and Ping home screen widget
subject: LinkedList Question
Similar Threads
doubt in offer()
PriorityQueue - ClassCastException in offer()
dynamically adding to queue
Linked List equals method
LinkedList using Generics