ArrayList Example in Java – How to use ArrayList in Java Program

ArrayList Example in Java. This Java ArrayList example describes the basic operations performed on the ArrayList in Collection Framework.

Program:

import java.util.*;
public class ArrayListDemo {
/**
* @param args
*/
public static void main(String[] args) {
ArrayList al = new ArrayList();
al.add(“1″);
al.add(“2″);
al.add(“3″);
System.out.println(“ArrayList size: “+al.size());
System.out.println(“ArrayList: “+al);
al.add(“4″);
al.add(4, “5″);
System.out.println(“Size after addition : ” + al.size());
al.remove(3);
al.remove(“5″);
System.out.println(“Size after removal : ” + al.size());
System.out.println(“ArrayList: “+al);
for(int i=0;i<al.size();i++)
System.out.println(“ArrayList-element["+i+"]:”+al.get(i));
}

Result:

ArrayList size: 3
ArrayList: [1, 2, 3]
Size after addition : 5
Size after removal : 3
ArrayList: [1, 2, 3]
ArrayList-element[0]:1
ArrayList-element[1]:2
ArrayList-element[2]:3