ArrayList is a resizable array implementation from the Java Collection Framework. It allows storing duplicate elements, maintains insertion order, and grows dynamically.
ArrayList internally uses a dynamic array. When the array is full, it creates a new array with larger size and copies the elements.
// Creating an ArrayList and adding elements
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList names = new ArrayList<>();
names.add("Ram");
names.add("Shyam");
names.add("Gita");
System.out.println(names);
}
}
[Ram, Shyam, Gita]
// Accessing, updating and removing elements
ArrayList numbers = new ArrayList<>();
numbers.add(10);
numbers.add(20);
numbers.add(30);
numbers.set(1, 25);
numbers.remove(0);
System.out.println(numbers);
Element at index 1 is updated, and the first element is removed from the list.
Type a value and add it to the list. Watch indices grow dynamically!