ArrayList is the most frequently used collection class in Java. It implements the List interface, backed internally by a dynamically resizing array allocated on the Heap.
When you instantiate ArrayList<String> list = new ArrayList<>();, Java creates a reference to an empty internal array: elementData = {}.
list.add("Java") expands the internal array to the default capacity of 10.System.arraycopy(), and the old array is reclaimed by the Garbage Collector.
import java.util.ArrayList;
import java.util.Collections;
public class ArrayListDemo {
public static void main(String[] args) {
// 1. Instantiating a generic ArrayList of Strings
ArrayList<String> frameworks = new ArrayList<>();
// 2. Adding elements (O(1) amortized time)
frameworks.add("Spring Boot");
frameworks.add("Hibernate");
frameworks.add("Micronaut");
frameworks.add("Quarkus");
// 3. Fast random access by index (O(1) time)
System.out.println("First framework: " + frameworks.get(0)); // Spring Boot
// 4. Updating an element
frameworks.set(2, "Jakarta EE");
// 5. Removing an element
frameworks.remove("Quarkus");
// 6. Checking existence and size
System.out.println("Contains Hibernate? " + frameworks.contains("Hibernate")); // true
System.out.println("Current Size: " + frameworks.size()); // 3
// 7. Sorting using Collections utility
Collections.sort(frameworks);
System.out.println("Sorted List: " + frameworks);
}
}
| Operation | Time Complexity | Underlying Mechanism |
|---|---|---|
get(index) | O(1) Constant | Direct memory pointer math: base + index * size |
add(element) | O(1) Amortized | Appends to end of internal array |
add(index, element) | O(n) Linear | Shifts all subsequent elements to the right |
remove(index) | O(n) Linear | Shifts all subsequent elements to the left |
contains(object) | O(n) Linear | Sequential linear search using equals() |
Now let us explore LinkedList, its doubly-linked node architecture, and when it outperforms ArrayList. Proceed to: Java LinkedList Class →
Accelerate your Java mastery by studying these tightly connected core tutorials:
Doubly linked list comparison.
Fixed-size array foundation.
Iterating through lists.
Sorting ArrayLists.