Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to iterate over elements of ArrayList in java

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 320
    Comment on it

    In this blog we will learn how to iterate over the elements of a collection (here we considered ArrayList) using generics and without generics.

    First we will go through the process of using iterator method. Iterator() method is included by every collection class. The iterator method returns an object of the Iterator interface. This object is used to access each element of the collection. iterator object goes through the following cycle to access the element:

    1- Invoke the collection’s iterator() method

    2- Now using a loop the instance of the iterator interface calls the hasNext() method — which returns true or false. If true is returned, it means the iterator object accesses the collection; else the looping stops.

    3- Now the object of the iterator interface calls the next() method that obtains every item in the collection.

    Example of ArrayList iteration without generics: Traversing Elements of ArrayList using iterator method:

    import java.util.*;
    class Arraylist {
    public static void main(String arg[]){
     ArrayList arrlist = new ArrayList();
     arrlist.add("India");
     arrlist.add("Sri Lanka");
     arrlist.add("Malaysia");
     arrlist.add("Singapore");
     arrlist.add("Bangladesh");
     Iterator itrate = arrlist.iterator();
    int count=1;
    while(itrate.hasNext()){
            System.out.println(count +".) "+ itrate.next());    
            Count++;
    }
    }    
    }

    We face this issue n Java 5 and later version if we use collections without using type specifiers (for example:we use Arraylist() instead of ArrayList<String>()).As this example was compiled using the java version "1.8.0_111" hence without using generics on compiling the Arraylist.java file we get the following notice:

    Note: Arraylist.java uses unchecked or unsafe operations.

    On executing the Arraylist.class file we receive the following output:

    Output:
    1.) India
    2.) Sri Lanka
    3.) Malaysia
    4.) Singapore
    5.) Bangladesh


    Example of ArrayList iteration using generics: Iterating Collection using for-each loop.
     

    import java.util.*;
    
    class Arraylistiterator {
        
    public static void main(String arg[]){
    
     ArrayList<String> arrlist = new ArrayList<String>();
     arrlist.add("First element");
     arrlist.add("Second element");
     arrlist.add("Third element");
     arrlist.add("Fourth element");
     arrlist.add("Fifth element");
     int count = 1;
     for(String obj: arrlist){
         System.out.println(count + ".)" + obj);
         count++;
     }
     
    }
        
    }
    
    Output:
    
    1.)First element
    2.)Second element
    3.)Third element
    4.)Fourth element
    5.)Fifth element

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: