Monday, 29 May 2017

Traverse Linked list using iterator

package javaRadarLinkedList;

import java.util.Iterator;
import java.util.LinkedList;

public class ItrTraversal {

      public static void main(String[] args) {
            //create Linked List
            LinkedList<String> javaRadarList = new LinkedList<String>();
            //Add elements to LinkedList
            javaRadarList.add("Java");
            javaRadarList.add("Jquery");
            javaRadarList.add("Spring");
            javaRadarList.add("Hibernate");
            javaRadarList.add("EJB");

            //creating iterator to traverse over javaRadarList
            Iterator<String> listIterator = javaRadarList.iterator();

            System.out.println("Traversal in forward direction");
            while(listIterator.hasNext()){
                  System.out.println(listIterator.next());
            }
      }

}

OUTPUT:
Traversal in forward direction
Java
Jquery
Spring
Hibernate
EJB 


No comments:

Post a Comment

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
If you are looking for a reference book on java then we recommend you to go for → Java The Complete Reference
Click on the image link below to get it now.