Iterate through elements of LinkedList using Iterator - Java Collection Framework

Java examples for Collection Framework:LinkedList

Description

Iterate through elements of LinkedList using Iterator

Demo Code


import java.util.Iterator;
import java.util.LinkedList;
 
 
public class Main {
 
  public static void main(String[] args) {
 
    //create LinkedList object
    LinkedList lList = new LinkedList();
   /*from   w  w  w  . j  av a2  s .  c om*/
    //add elements to LinkedList
    lList.add("1");
    lList.add("2");
    lList.add("3");
    lList.add("4");
    lList.add("5");
   
    Iterator itr = lList.iterator();
   
    System.out.println("Iterating through elements of Java LinkedList...");
    System.out.println("LinkedList contains : ");
    while(itr.hasNext())
    {
      System.out.println(itr.next());
    }
 
  }
}

Result


Related Tutorials