ArrayList Example Java Program


The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed. Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold. Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.


ArrayList( )
ArrayList(Collection Variable_name)
ArrayList(Data_type capacity)

ArrayList Example Program

import java.util.*;
class ArrayListExample{
	public static void main(String[] args){
		ArrayList al=new ArrayList();
		System.out.println("Initial size of Arraylist is "+al.size());
		System.out.println("Arraylist is "+al);
		System.out.println("New size of Arraylist is "+al.size());
		al.add(1,"How are you");
		System.out.println("New Arraylist is "+al);
		System.out.println("New size of Arraylist is "+al.size());

Sample Output

Output is:
Initial size of Arraylist is 0
Arraylist is [Hai, Hello]
New size of Arraylist is 2
New Arraylist is [Hai, How are you, Hello]
New size of Arraylist is 3