The java.util.ArrayList class is a resizable-array implementation of the List interface. It implements all optional list operations and permits all elements, including null. Beyond implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list.
A traditional Array is a static data structure. Once you define int[] arr = new int[5], your memory footprint is locked. An ArrayList is dynamic. It manages a private array internally and handles all the complex logic of expanding that array when it gets full.
When you add an element to an ArrayList and the internal array is already full, the following "Growth Policy" triggers:
System.arraycopy() to move all elements from the old array to the new one.Capacity vs. Size: The size is how many items are in the list. The capacity is how many items the internal array can hold before it needs to grow.
To write efficient code, you must understand the "Big O" complexity of ArrayList operations:
| Operation | Complexity | Reason |
|---|---|---|
| Get by Index | $O(1)$ | Direct memory calculation. |
| Add (at end) | $O(1)$ amortized | Usually just adding to the next slot. |
| Add (at index) | $O(n)$ | Must shift all subsequent elements to the right. |
| Remove (at index) | $O(n)$ | Must shift all subsequent elements to the left. |
| Search (by value) | $O(n)$ | Must scan every element one-by-one. |
In older versions of Java, ArrayLists held Object, which meant you had to manually "cast" your data. Since Java 5, we use Generics to ensure type safety.
ArrayList<String> list = new ArrayList<>(); // Correct waylist.add("Java");String s = list.get(0); // No casting needed
This example demonstrates sorting, filtering, and the use of custom objects within an ArrayList.
for loop using list.remove(i). This shifts the indices and causes you to skip the next item. Always use Iterator.remove() or removeIf().ArrayList<Integer> incurs a cost called "Auto-boxing." If you are storing millions of numbers, a primitive array (int[]) is significantly more memory-efficient.new ArrayList<>(10000). This prevents the costly "Growth Policy" from triggering 15+ times.Q: Is ArrayList thread-safe?
A: No. If multiple threads access an ArrayList concurrently and at least one thread modifies it, it must be synchronized externally. CopyOnWriteArrayList is a thread-safe alternative.
Q: What is the difference between length, length(), and size()?
A: length is a property of arrays. length() is a method of Strings. size() is a method of Collections like ArrayList.
Q: How do you make an ArrayList Read-Only?
A: Use Collections.unmodifiableList(myList). Any attempt to add() or remove() will throw an UnsupportedOperationException.
The ArrayList is the ultimate balance between performance and convenience. For 90% of Java development tasks involving a list of items, ArrayList is the correct choice. It provides the speed of an array for reads while handling the complexity of growth for writes. Master its performance trade-offs, and you master Java data management.