ArrayList
ArrayList is one of the most widely used classes in the Java Collections Framework. It is a resizable array implementation of the List interface, providing fast random access and flexible sizing. This is a very high-frequency interview topic.
What Is ArrayList?
- Resizable array implementation of List
- Maintains insertion order
- Allows duplicate elements
- Allows multiple null values
- Part of java.util package
ArrayList<String> list = new ArrayList<>();
Position in Collection Hierarchy
Iterable
└── Collection
└── List
└── ArrayList
Key Characteristics of ArrayList
- Backed by a dynamic array
- Fast random access (O(1))
- Slower insertions/deletions in middle
- Not synchronized (not thread-safe)
How ArrayList Works Internally (Interview Favorite)
- Uses an internal Object[] array
- Default capacity = 10
- Grows by 50% when capacity is exceeded
- Old array copied into a new larger array
Creating an ArrayList
1️⃣ Default Constructor
ArrayList<Integer> list = new ArrayList<>();
2️⃣ With Initial Capacity
ArrayList<Integer> list = new ArrayList<>(50);
✔ Improves performance when size is known
3️⃣ From Another Collection
ArrayList<Integer> list = new ArrayList<>(existingList);
Commonly Used Methods
Adding Elements
list.add(10);
list.add(1, 20);
Accessing Elements
int value = list.get(0);
Updating Elements
list.set(0, 100);
Removing Elements
list.remove(0); // by index
list.remove(Integer.valueOf(10)); // by value
Size and Search
list.size();
list.contains(20);
list.indexOf(20);
Iterating an ArrayList
Using for-each loop
for (Integer i : list) {
System.out.println(i);
}
Using Iterator
Iterator<Integer> it = list.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
Using ListIterator
ListIterator<Integer> li = list.listIterator();
Performance Complexity (Important)
| Operation | Time Complexity |
|---|---|
| get/set | O(1) |
| add (end) | O(1) amortized |
| add (middle) | O(n) |
| remove (middle) | O(n) |
| search | O(n) |
ArrayList vs Array (Quick Comparison)
| Aspect | Array | ArrayList |
|---|---|---|
| Size | Fixed | Dynamic |
| Type | Primitive + Object | Object only |
| Methods | Limited | Rich API |
| Performance | Faster | Slight overhead |
Thread Safety
- ArrayList is not synchronized
- Not safe for concurrent modification
Thread-safe Alternatives: Collections.synchronizedList(), CopyOnWriteArrayList
Common Beginner Mistakes
- Using ArrayList in multi-threaded context without synchronization
- Removing elements in loop incorrectly
- Forgetting generics
- Frequent insertions in middle
- Using raw ArrayList type
Interview-Ready Answers
Short Answer
ArrayList is a resizable array implementation of the List interface.
Detailed Answer
In Java, ArrayList is a dynamic array-based implementation of the List interface. It allows duplicates, maintains insertion order, and provides fast random access. It is not synchronized and is best suited for scenarios with frequent read operations.
Key Takeaway
Use ArrayList when fast access and iteration are required. Avoid it when frequent insertions/deletions in the middle or thread safety is a concern.