Tuesday, November 6, 2012

How to use get Method in ArrayList in Java

/*
List interface defines the method as - Object get(int index)
*/
import java.util.ArrayList;
public class getMethod {

    public static void main(String[] args) {
        ArrayList a1 = new ArrayList();
        for(int i=1;i<=3;i++)
        {
            a1.add(i);
        }
       
        System.out.println("The size of a1 is " + a1.size());
        for(int i=0;i<a1.size();i++)
        {
            System.out.println("The Value of a1 is " + a1.get(i));
        }
    }

}

Answer
----------
The size of a1 is 3
The Value of a1 is 1
The Value of a1 is 2
The Value of a1 is 3

No comments:

Post a Comment