Clean ⢠Professional
ArrayList is one of the most widely used classes in Java, found in the java.util package.
It is a resizable (dynamic) array implementation of the List interface and stores elements in indexed, ordered, and growable form.
Iterable
ā
Collection
ā
List
ā
ArrayList
This means ArrayList inherits methods from Iterable ā Collection ā List.
An ArrayList is a dynamic array that:
Unlike normal arrays, whose size is fixed, an ArrayList can expand or shrink at runtime.
| Feature | Description |
|---|---|
| Dynamic Size | Automatically grows when elements are added. |
| Indexed Access | Get/set values using indexes like arrays. |
| Allows Duplicates | Same value may appear multiple times. |
| Ordered Collection | Maintains insertion order. |
| Fast Access (O(1)) | Very fast for reading (get/set). |
| Slower Insert/Delete | Slow when inserting/removing in the middle. |
| Not Synchronized | Not thread safe (use Vector instead if needed). |
Internal Structure:
ArrayList internally uses a dynamic array (Object[]) to store elements.
transient Object[] elementData;
How Resizing Works?
When ArrayList becomes full and you add a new element:
Resize Formula (Java 8+):
newCapacity = oldCapacity + (oldCapacity / 2)
ā”ļø That's why append operations (add()) are amortized O(1).
The ArrayList class provides three main constructors:

The default constructor creates an empty ArrayList with no elements, but with an initial capacity that becomes 10 when the first element is added.
ArrayList<String> list = new ArrayList<>();
Internal Behavior
Creates an ArrayList with a given initial capacity, useful to avoid repeated resizing.
ArrayList<Integer> list = new ArrayList<>(50);
Creates a new ArrayList and copies all elements from an existing collection.
ArrayList<String> list2 = new ArrayList<>(list1);
Internal Behavior
Example
List<String> list1 = List.of("A", "B", "C");
ArrayList<String> list2 = new ArrayList<>(list1);
⤠Add Elements
Used to insert new elements into the list; when an index is given, the element is added at that specific position.
list.add("A");
list.add(1, "B"); // insert at index
⤠Access Elements
Retrieves the element stored at a specific index in the list.
list.get(0);
⤠Update Elements
Replaces an existing element at the given index with a new value.
list.set(1, "Updated");
⤠Remove Elements
Deletes elements from the listāeither by index or by value.
list.remove(0); // by index
list.remove("A"); // by value
⤠Size
Returns the total number of elements currently stored in the list.
list.size();
⤠Search
Checks whether a given element exists in the list (returns true/false).
list.contains("A");
⤠Clear
Removes all elements from the list, making it completely empty.
list.clear();
⤠isEmpty
Checks whether the list contains no elements.
list.isEmpty();
⤠indexOf
Returns the index of the first occurrence of an element, or ā1 if not found.
list.indexOf("A");
⤠lastIndexOf
Returns the index of the last occurrence of the specified element.
list.lastIndexOf("A");
⤠toArray
Converts the list elements into a normal array.
String[] arr = list.toArray(new String[0]);
For-each Loop
A simple and clean way to iterate through all elements in sequential order.
for (String s : list) {
System.out.println(s);
}
Iterator
A safe way to traverse the list, which also allows element removal through the iterator.
Iterator<String> itr = list.iterator();
while (itr.hasNext()) {
System.out.println(itr.next());
}
Normal For Loop
Allows traversal using index values, useful when positions matter.
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
ListIterator (Advanced & Important)
Allows forward and backward traversal of the list.
ListIterator<String> litr = list.listIterator();
while (litr.hasNext()) {
System.out.println(litr.next());
}
| Operation | Complexity | Notes / Definition |
|---|---|---|
| get() | O(1) | Accesses an element directly using its index, which requires no traversal. |
| set() | O(1) | Updates the value at a specific index instantly without shifting elements. |
| add(value) | O(1) amortized | Appends element at the end; occasionally resizing may happen, but average is constant-time. |
| add(index) | O(n) | Inserting at any position other than the end requires shifting elements to make space. |
| remove(index) | O(n) | Removing an element forces all elements after it to shift left. |
| contains() | O(n) | Searches element sequentially because ArrayList does not support binary search unless sorted. |
Use ArrayList when:
Avoid ArrayList when:
Collections.synchronizedList(list)
| Feature | ArrayList | LinkedList |
|---|---|---|
| Structure | Dynamic Array | Doubly Linked List |
| Access | Fast (O(1)) | Slow (O(n)) |
| Insert/Delete Middle | Slow | Fast (O(1)) if node known |
| Memory | Less | More (extra pointers) |
import java.util.ArrayList;
public class Example {
public static void main(String[] args) {
ArrayList<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Mango");
System.out.println(fruits); // [Apple, Banana, Mango]
fruits.set(1, "Orange");
System.out.println(fruits.get(0)); // Apple
fruits.remove("Mango");
System.out.println(fruits); // [Apple, Orange]
}
}
Before resizing (capacity 5)
Index: 0 1 2 3 4
-------------------------------------
Array: | A | B | C | null | null |
-------------------------------------
After resizing (capacity 7)
New Capacity = 5 + 2 = 7
Index: 0 1 2 3 4 5 6
-----------------------------------------------------
Array: | A | B | C | null | null | null | null |
-----------------------------------------------------