Wednesday, May 23, 2012

Program for ArayList in Java

import java.util.ArrayList; 
public class Demoarraylist 
{
public static void main(String[] args) 
{
     ArrayList<String> al = new ArrayList<String>();
     System.out.println("Initial size of ArrayList : " + al.size());
     al.add("C");
     al.add("A");
     al.add("E");
     al.add("B");
     al.add("D");
     al.add("F");
     System.out.println("Contents of ArrayList : " + al);
     System.out.println("Adding A2 in index 1");
     al.add(1, "A2");
     System.out.println("Size of ArrayList after additions of A2 is : " + al.size());
     System.out.println("Contents of al: " + al);
     al.remove("F");
     al.remove(2);
     System.out.println("Size of ArrayList after deletions of F and remove index 2 : " + al.size());
     System.out.println("Contents of al: " + al);
}

}

Output:

Initial size of ArrayList : 0
Contents of ArrayList : [C, A, E, B, D, F]
Adding A2 in index 1
Size of ArrayList after additions of A2 is : 7
Contents of al: [C, A2, A, E, B, D, F]
Size of ArrayList after deletions of F and remove index 2 : 5
Contents of al: [C, A2, E, B, D]

No comments:

Post a Comment

Followers