Java ArrayList

ArrayList is a class which comes with Java Collections package and it is one of the concrete implementations of List interface. This class was first introduced in Java version 1.2

ArrayList is backed by the dynamic array in terms of the array size. This dynamic array means array size will grow, when we keep on adding the Objects/elements into the ArrayList.

ArrayList mainly used to store the elements/Objects as a array data structure and helps to perform the operations over the stored elements/objects.

Behavior of Java ArrayList Class

ArrayList Constructors

Backed by array size will grow dynamically when programmer keeps on adding the elements or Objects to the ArrayList.


ArrayList Array

If we check next image, numbers started from 0 to 8 these are called as indexes. We can insert elements or objects based on the which index needs to inserted.

java arraylist array

Java ArrayList Example

import java.util.ArrayList;
import java.util.List;

public class ArrayListExample {

	public static void main(String[] args) {
		
		List list=new ArrayList();
		list.add("Position zero");
		list.add("Position one");
		list.add("Position two");
		list.add("Position three");
		list.add("Position four");
		System.out.println(list.toString());
		list.add(4,"inserted New Element at 4th position");
		System.out.println(list.toString());
	}
}
Output :-
[Position zero, Position one, Position two, Position three, Position four]
[Position zero, Position one, Position two, Position three, inserted New Element at 4th position, Position four]