100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Java

ArrayList in Java

Learn how ArrayList works internally, its time complexity, and when to use it over arrays or LinkedList.

Collections FrameworkBeginner8 min readJul 7, 2026
Analogies

1. Introduction

ArrayList is a resizable-array implementation of the List interface. Unlike a plain Java array whose size is fixed at creation, an ArrayList automatically grows as elements are added, making it one of the most commonly used collection types.

🏏

Cricket analogy: An ArrayList is like a squad list that can keep adding reserve players mid-tournament, unlike a fixed 11-man playing XI announced once and locked for the match.

Internally, ArrayList stores elements in a contiguous Object[] array. When the array is full, a new, larger array (typically 1.5x the current capacity) is allocated and the old elements are copied over.

🏏

Cricket analogy: Internally an ArrayList is like a dugout bench that starts with 10 seats; once a team signs an 11th reserve, groundstaff bring a new 15-seat bench and move everyone over.

2. Syntax

text
List<String> list = new ArrayList<>();
List<Integer> withCapacity = new ArrayList<>(20); // initial capacity
list.add("A");
list.get(0);
list.set(0, "B");
list.remove(0);
list.size();

3. Explanation

Because ArrayList is backed by an array, accessing an element by index — get(i) or set(i, x) — is O(1) since it's a direct array offset lookup. Inserting or removing an element in the middle is O(n) because all subsequent elements must be shifted to fill or create the gap.

🏏

Cricket analogy: Looking up batsman #4 in the ArrayList lineup is instant like reading a scorecard slot directly, but inserting a new batsman at position 3 means shifting every player below down one slot, an O(n) reshuffle.

ArrayList implements List, so it preserves insertion order and allows duplicate elements. It is not synchronized, meaning it is not thread-safe by default — use Collections.synchronizedList() or CopyOnWriteArrayList for concurrent access.

🏏

Cricket analogy: An ArrayList batting order keeps players in the exact sequence they were added and allows the same player's name to appear twice by mistake, but two scorers editing it simultaneously without Collections.synchronizedList risk a torn scorecard.

Amortized append (add at the end) is O(1) on average, even though occasional resizing operations are O(n), because resizing happens infrequently as capacity doubles/grows by 1.5x.

4. Example

java
import java.util.*;

public class ArrayListDemo {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>();
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");
        names.add(1, "David"); // insert at index 1, shifts others

        System.out.println("List: " + names);
        System.out.println("Get index 2: " + names.get(2));

        names.remove("Bob"); // O(n) - shifts remaining elements
        System.out.println("After remove: " + names);
        System.out.println("Size: " + names.size());
    }
}

5. Output

text
List: [Alice, David, Bob, Charlie]
Get index 2: Bob
After remove: [Alice, David, Charlie]
Size: 3

6. Key Takeaways

  • ArrayList is backed by a resizable Object[] array.
  • get(index) and set(index, value) are O(1).
  • Insert/remove in the middle is O(n) due to element shifting.
  • Maintains insertion order and allows duplicate elements.
  • Not synchronized — not thread-safe without external synchronization.

Practice what you learned

Was this page helpful?

Topics covered

#Java#JavaProgrammingStudyNotes#Programming#ArrayListInJava#ArrayList#Syntax#Explanation#Example#StudyNotes#SkillVeris