Sunday 17 May 2015

How iterate LinkedList by using iterator

import java.util.Iterator;

import java.util.LinkedList;

public class LinklistIterator {


    public static void main(String[] args) {
       
        LinkedList<String> linkedList = new LinkedList<String>();
        linkedList.add("Mumbai");
        linkedList.add("Delhi");
        linkedList.add("Noida");
        linkedList.add("Gao");
        linkedList.add("Patna");
       
        Iterator<String>  itr = linkedList.iterator();
         while (itr.hasNext()) {
            System.out.println("Element is ="+itr.next());
           
        }
   
   
    }
   
}

 Output:

Element is =Mumbai

Element is =Delhi

Element is =Noida

Element is =Gao

Element is =Patna


No comments:

Post a Comment