ARRAYLIST ITERATOR

ArrayList Iterator


How To Access or Iterate elements from ArrayList?

    1.  By Using get():

    • If we want to get the data from ArrayList there is one method it’s named as get().

    • get() is one of the important method inside Collections.

    • By using get() we can able to access the value by passing the specified index location.

    • It acts same as charAt() inside string class.

Syntax:

    public Object get(int index)

Example 1:

 
						
              package com.sdj.pack1;
              public class ArrList
              {
                 public static void main(String[] args)
                 {
                    ArrayList l1=new ArrayList();
                    l1.add(10);
                    l1.add(20);
                    l1.add(30);
                    System.out.println(l1.get(1));
                    int i=l1.get(2);
                    
                    /* Here compilation fails  why  because get() will */
                    /* return a value of  Object class type but  we are */
                    /* trying to assign to ‘int’ primitive type. This is mismatch. */
                    
                    int j=(Integer)l1.get(2);
                    String s=(String)l1.get(0);// here will get exception like bellow
                    
                    /* Exceptioninthread"main" java.lang.ClassCastException: */
                    /* java.lang.Integeratcom.sdj.pack1.ArrList.main(ArrList.java:18). */
                    /* Why bcz we are receiving Integer object from index=0 but we are */
                    /* converting  to String type which isn’t possible */
                    System.out.println(l1.get(4));
                 }
              } 
                 	

Output:

     20
     
     

Iterating List with for-loop:

Example 2:

 
						
               package com.sdj.pack1;
               public class ArrList
               {
                  public static void main(String[] args)
                  {
                     ArrayList l1=new ArrayList();
                     l1.add(10);
                     l1.add(20);
                     l1.add(30.25);
                     l1.add("abc");
                     l1.add("ram");
                     l1.add(999);
                     for(int i=0;i<l1.size();i++)
                     {
                        System.out.println(l1.get(i));
                     }
                  }
               }                

Output:

10 20 30.25 abc ram 999

Example 3:

 
						
               package com.sdj.pack1;
               public class ArrList
               {
                  public static void main(String[] args)
                  {
                     ArrayList l1=new ArrayList();
                     l1.add(10);
                     l1.add(20);
                     l1.add(30.25);
                     l1.add("abc");
                     l1.add("ram");
                     l1.add(999);
                     for(int i=0;i<l1.size();i++)
                     {
                        System.out.println(l1.get(i));
                     }
                  }
               }			       

Output:

     20
     
     

How to find out datatype of receiving value at runtime:

instaceOf():

    We can find out data type for the given value by using one special operator and it is named as “instanceof”.

Syntax:

    recValinstanceofdatatype;

  • In the above syntax “datatype ” refers “derived types” only.
  • instanceof can supports only derived types.
  • instanceof each entity is called as Object.
  • instanceof will return one boolean value as output i.e. TRUE/FLASE.
  • If received value matches to the given datatype then instanceof operator will return output as TRUE otherwise it will return FLASE.

Example 4:

 
						
               package com.sdj.pack1;
               public class ArrList
               {
                  public static void main(String[] args)
                  {
                     ArrayList l1=new ArrayList();
                     l1.add(10);
                     l1.add(20);
                     l1.add(30.25);
                     l1.add("abc");
                     l1.add("ram");
                     l1.add(999);
                     for(int i=0;i<l1.size();i++)
                     {
                        Object listval=l1.get(i);
                        if(listval instanceof Integer)// we are checking input value is Integer or not
                        {
                           int j=(Integer)listval;
                           System.out.println(j+10);
                        }
                        else if(listval instanceof String)// we are checking input value is String or not
                        {
                           String s=(String)listval;
                           System.out.println(s+"guna");
                        }
                        else if(listval instanceof Double)
                        /* we are checking input value is Double or not */
                        {
                           double d=(Double)listval;
                           System.out.println(d+10.25f);
                        }
                     }
                  }
               }                

Output:

20 30 40.5abcgunaramguna 1009
    Note

    Suppose If we want to sort the saFrom above example, if we receive input values as Integer or String or Double then we won’t get any problem it will execute corresponding if block.

Example 5:

 
						
               package com.sdj.pack1;
               public class ArrayListDemo
               {
                  public static void main(String[] args)
                  {
                     ArrayList list=new ArrayList(3);
                     list.add(10);
                     list.add(30);
                     list.add("jaipur");
                     int i=0;
                     while(i<list.size())
                     {
                        System.out.println(list.get(i));
                        i++;
                     }
                  }
               }              

Output:

10 30 jaipur

Iterating the elements by using Iterator Interface:

  • An Iterator is one of the interface inside java.util package.

  • It contains 3 important methods.They are

    • hasNext():

      • it will check either List object contain the elements or not, If Iterator receive atleast one element from List object then it will return boolean “TRUE” else return FALSE i.e.
        list.size() > 0 = True else False

    • next():

      • It will perform two operations

        • It will take ctrl to one step forward from current position, if we have next position.
        • And return that position element to user.

    • remove():

      • It will remove the current position element from List object.

    Note
      Iterator initially points the starting position ,there we can’t do any operation why because in Iterator data will store from 0th index position. If we want to do any operation for Iterator object we should use next(). Even we use remove() for removing element. if we use next() for the first time then ctrl will move to 0th (zero) position from starting position and it will return 0th position value to user.
  • By using Iterator interface we can iterate List Objects in forward direction only but can’t to back word direction.
  • We can remove elements from List object , But we can’t add/update new element to the List Object.
  • If we want to get Iterator Object from the List Object we should call iterator() method.

iterator():

  • iterator() is one of the important method inside ArrayList.
  • Its return type is Iterator interface.
    • Example

      List list= newarrayList();
      java.util.Iterator it = list.iterator();

Example 6:

 
						
               package com.sdj.pack1;
               public class ArrayListDemo
               {
                  public void printList(ArrayList lis)
                  {
                     Iterator it =lis.iterator();
                     while(it.hasNext())
                     {
                        System.out.println(it.next());
                     }
                  }
                  public static void main(String[] args)
                  {
                     ArrayList list=new ArrayList(3);
                     list.add(10);
                     list.add(30);
                     list.add("jaipur");
                     ArrayList Demomyclass=new ArrayListDemo ();
                     myclass.printList(list);  /* calling println method from main() */
                  }
               }                

Output:

10 30 jaipur

Example 7:

 
						
               package com.sdj.pack1;
               public class ArrayListDemo
               {
                  public static void main(String[] args)
                  {
                     ArrayList list=new ArrayList(3);
                     list.add(10);
                     list.add(30);
                     list.add("jaipur");
                     Iterator it =lis.iterator();
                     while(it.hasNext())
                     {
                        System.out.println(it.next());
                     }
                  }
                  /* Here next() will shift the iterator position from current */
                  /* to  one position forward . And return that position value to user. */
               }                

Output:

10 30 jaipur

remove():

remove() can remove the current element from the list.

Example 8:

 
						
               package com.sdj.pack1;
               public class ArrayListDemo
               {
                  public void printList(ArrayListlis)
                  {
                     System.out.println("list size:" +lis.size());
                     Iterator it=lis.iterator();
                     while(it.hasNext())
                     {
                        System.out.println(it.next());
                        it.remove();
                        System.out.println("list size:" +lis.size());
                     }
                  }
                  public static void main(String[] args)
                  {
                     ArrayList list=new ArrayList(3);
                     list.add(10);
                     list.add(30);
                     list.add("praveen");
                     list.add("SDJ");
                     list.add(333);
                     list.add("pqr");
                     ArrayListDemo u=new ArrayListDemo();
                     u.printList(list);
                  }
               }                

Output:

list size:6 ----- size of the list before calling remove() 10 list size:5 ----- size of the list after calling remove() 30 list size:4 ----- size of the list after calling remove() for next iteration ----- ----- list size:1 pqr list size:0
    Note
      Iterator remove() will remove the element directly from the List object.