Appends the specified element to the end of this list.
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
Example of: add(E e)

package com.logicbig.example.arraylist;
import java.util.ArrayList;
public class AddExample {
public static void main(String... args) {
// Add single element
ArrayList<String> list = new ArrayList<>();
list.add("First");
list.add("Second");
System.out.println("List after adds: " + list);
System.out.println("Size: " + list.size());
// Output: List after adds: [First, Second]
// Output: Size: 2
}
}
Output
List after adds: [First, Second]
Size: 2
JDK 25Example of: add(int index, E element)

package com.logicbig.example.arraylist;
import java.util.ArrayList;
import java.util.Arrays;
public class AddExample2 {
public static void main(String... args) {
// Insert at specific position
ArrayList<String> list = new ArrayList<>(Arrays.asList("A", "C", "D"));
list.add(1, "B");
System.out.println("After insert: " + list);
}
}
Output
After insert: [A, B, C, D]
JDK 25