Collections Enhanced new for loop


This is the second tutorial on the enhanced for loop on collections. Earlier one is with arrays – Arrays Enhanced for loop (foreach).

Collections Enhanced new for loop

Observe the following program.

import java.util.ArrayList;
public class ForEachCollections
{
  public static void main(String args[])
  {                                           // enhanced for loop with generics Integer array list
    ArrayList al1 = new ArrayList();
    al1.add(100);
    al1.add(200);     
    al1.add(300);

    System.out.print("Printing int array list with taditional for loop: ");
    for(int i = 0 ; i < al1.size(); i++)
    {
      System.out.print(al1.get(i) + "  ");
    }

    System.out.print("\nPrinting int array list with enhanced for loop: ");
    for(int k : al1)
    {
      System.out.print(k + "  " );
    }
		                              // enhanced for loop with generics String array list	
    ArrayList al2 = new ArrayList();
    al2.add("Sridhar"); 
    al2.add("Jyothi");
    al2.add("Jyothna");
  
    System.out.print("\n\nPrinting string array list with taditional for loop: ");
    for(int i = 0 ; i < al2.size(); i++)
    {
      System.out.print(al2.get(i) + "  ");
    }

    System.out.print("\nPrinting string array list with enhanced for loop: ");
    for(String str : al2)
    {
      System.out.print(str + "  " );
    }
  }
}

Collections Enhanced new for loop

A generic ArrayList al1 that stores int values and al2 storing string values are created and elements are added.

       for(int k : al1)
       {
         System.out.print(k + "  " );
       }

Make a note, the data type stored by array list and the variable in the for loop are the same.

Collections Enhanced new for loop with Objects

Generics store only one type of data like all integers or all doubles or all Student objects etc. To store all types of objects like integers, doubles, Dates, Employees, a generic type Object can be taken as every object can be converted into the super-most class object Object.

To store any object, the loop can be modified as follows.

import java.util.*;
public class Demo
{
  public static void main(String args[])
  {
    ArrayList al3 = new ArrayList< Object >();
    al3.add(new Integer(10)); 
    al3.add(new Double(10.5));
    al3.add(new Date());
    al3.add("Jyothna");
  
    for(Object obj1 : al3)
    {
      System.out.print(obj1 + "  " );
    }
  }
}

Collections Enhanced new for loop

       for(Object obj1 : al3)
       {
         System.out.print(obj1 + "  " );
       }

Every object of Integer, Double, Date and String are stored in obj1. In each iteration, obj1 is printed.

2 thoughts on “Collections Enhanced new for loop”

  1. import java.util.*;
    class ArrayListTest
    {
    public static void main(String… s)
    {
    ArrayList al=new ArrayList();
    al.add(“amit”);
    al.add(“deepak”);
    al.add(“Rahul”);
    al.add(“Sanjay”);
    al.add(“Sachin”);
    al.add(“rahul”);
    al.add(new Integer(110));
    Iterator itr=al.iterator();
    while(itr.hasNext())
    {
    Object ob=itr.next();
    String str=(String)ob;
    System.out.println(str);
    }
    }
    }
    sir ismae ClassCast exception aa rhi hai but why ?

Leave a Comment

Your email address will not be published.