Tutorials

Java Collection Programs


ArrayList Traversal


In this chapter our task is:

  • We will create an arraylist
  • We will add few elements in arraylist
  • We will iterate over arraylist using either iterator or enumeration.

We will also see the way to traverse List using List iterator and its difference from iterator at the end of this chapter.

See the code snippet below:

 

Example: Traverse ArrayList using Iterator



package javaRadarArrayList;

import java.util.ArrayList;
import java.util.Iterator;

public class ItrTraversal {

       public static void main(String[] args) {

       //create Array List
       ArrayList javaRadarList = new ArrayList();

      //Add elements to ArrayList
       javaRadarList.add("Java");
       javaRadarList.add("Java");
       javaRadarList.add("Spring");
       javaRadarList.add("Hibernate");
       javaRadarList.add("EJB");

       //creating iterator to traverse over javaRadarList

       Iterator listIterator = javaRadarList.iterator();

          while(listIterator.hasNext()){

              System.out.println(listIterator.next());

          }

     }

}

OUTPUT

Java
Java
Spring
Hibernate
EJB
 
Example: Traverse ArrayList using Enumeration
 
package javaRadarArrayList;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.ListIterator;

public class EnumerationTraversal {

        public static void main(String[] args) {

        //create Array List
        ArrayList javaRadarList = new ArrayList();

        //Add elements to ArrayList
        javaRadarList.add("Java");
        javaRadarList.add("Jquery");
        javaRadarList.add("Spring");
        javaRadarList.add("Hibernate");
        javaRadarList.add("EJB");

         //creating iterator to traverse over javaRadarList
         Enumeration listEnumerator = Collections.enumeration(javaRadarList);

         System.out.println("List traversal using Enumeration");

          while(listEnumerator.hasMoreElements()){

              System.out.println(listEnumerator.nextElement());

           }

     }

}

OUTPUT:

List traversal using Enumeration
Java
Jquery
Spring
Hibernate
EJB
 
Lists like ArrayList, LinkedList can only be traversed using ListIterator. Set & Map cannot be traversed using ListIterator

ExampleTraverse ArrayList using ListIterator

ListIterator can traverse a list in both the forward and the backward direction. 

Iterator can only traverse in forward direction.

The code snippet shown below helps to traverse the list in both the forward and backward direction.

See the code snipet shown below.

package javaRadarArrayList;

import java.util.ArrayList;
import java.util.ListIterator;

public class ListItrTraversal {

        public static void main(String[] args) {

        //create Array List
          ArrayList javaRadarList = new ArrayList();

        //Add elements to ArrayList
         javaRadarList.add("Java");
         javaRadarList.add("Jquery");
         javaRadarList.add("Spring");
         javaRadarList.add("Hibernate");
         javaRadarList.add("EJB");

         //creating iterator to traverse over javaRadarList
          ListIterator listIterator = javaRadarList.listIterator();
                          
          System.out.println("Traversal in forward direction");

          while(listIterator.hasNext()){

              System.out.println(listIterator.next());

          }

                              
         System.out.println("Traversal in backward direction");

          while(listIterator.hasPrevious()){

              System.out.println(listIterator.previous());

          }

     }

}

OUTPUT

Traversal in forward direction
Java
Jquery
Spring
Hibernate
EJB
Traversal in backward direction
EJB
Hibernate
Spring
Jquery
Java
 

 



Please Share this page
Views : 73
Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!