ArrayList is a resizable, dynamic array implementation of the List interface in Java.
It allows duplicate elements, maintains insertion order, and provides fast random access.
Located in:
java.util.ArrayListCompared to traditional arrays:
| Feature | Array | ArrayList |
|---|---|---|
| Size | Fixed | Dynamic |
| Type | Primitives/Objects | Objects only |
| Flexibility | Low | High |
| Methods | None | Many built-in methods |
ArrayList is preferred when:
- Size is unknown
- Fast access is needed
- Random access is important
ArrayList internally uses a dynamic array (Object[] array).
- Initially: 10
- When full: Capacity grows by 1.5x
Formula:
newCapacity = oldCapacity + oldCapacity/2
Old capacity = 10 New capacity = 10 + 5 = 15
- Array becomes full
- New array (1.5x size) is created
- Old elements copied → new array
- The old array is discarded
This copying is expensive → avoid excessive expansion.
ArrayList<String> list = new ArrayList<>();With initial capacity:
ArrayList<String> list = new ArrayList<>(50);list.add("A");
list.add("B");
list.add(1, "C"); // insert at indexString x = list.get(0);list.set(1, "Z");list.remove(0); // by index
list.remove("A"); // by objectlist.contains("X");
list.isEmpty();
list.size();Object[] arr = list.toArray();for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}for (String s : list) {
System.out.println(s);
}Iterator<String> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}list.forEach(System.out::println);ArrayList<Integer> nums = new ArrayList<>();
nums.add(10); // autoboxingEnsures type safety.
| Operation | Time Complexity | Notes |
|---|---|---|
| get(index) | O(1) | Fast random access |
| set(index) | O(1) | |
| add(element) | O(1) (amortized) | Expansion costs O(n) |
| add at index | O(n) | Shifting needed |
| remove(index) | O(n) | Shifting needed |
| contains() | O(n) | Linear search |
| iteration | O(n) |
So ArrayList is fast for reading but slow for inserting/deleting in the middle.
-
Slow for insert/remove at middle (shifting)
-
Not thread-safe
-
Increases capacity automatically (memory overhead)
-
Can store only objects, not primitives
ArrayList is not thread-safe, but we can make it synchronized:
List<String> syncList = Collections.synchronizedList(new ArrayList<>());Or use CopyOnWriteArrayList (thread-safe):
CopyOnWriteArrayList<String> list = new CopyOnWriteArrayList<>();| Feature | ArrayList | LinkedList |
|---|---|---|
| Internal Structure | Dynamic Array | Doubly Linked List |
| Access by Index | O(1) | O(n) |
| Insert/Delete Middle | Slow | Fast |
| Memory | Less | More |
| When to Use | Frequent access | Frequent insert/delete |
| Feature | ArrayList | Vector |
|---|---|---|
| Thread-safe? | No | Yes |
| Performance | Faster | Slower |
| Growth | 1.5x | 2x |
| Legacy? | No | Yes |
import java.util.*;
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("Java");
list.add("Python");
list.add("C++");
list.add(1, "JavaScript");
list.remove("Python");
System.out.println("Size: " + list.size());
System.out.println("Elements:");
for (String s : list) {
System.out.println(s);
}
}
}Because it provides O(1) random access using index.
By 1.5x capacity (from Java 8 onwards).
No, but can be made synchronized.
Yes.
Use correct initial capacity.
-
ArrayList is a dynamic array with fast random access.
-
Allows duplicates and maintains insertion order.
-
Expands automatically when full.
-
Not good for frequent insert/delete in mid positions.
-
Use
Collections.synchronizedListorCopyOnWriteArrayListfor thread safety. -
Internal working uses Object[] and grows by 1.5x.