DEV Community

Jayashree
Jayashree

Posted on

ArrayList vs LinkedList in Java – Simple Explanation

When working with Java collections (especially from the Java Collections Framework), two commonly used classes are ArrayList and LinkedList. Both are part of the List interface, but they behave very differently internally.

Let’s break it down in a simple way.

What is an ArrayList?

An ArrayList is like a dynamic array.

  • Internally uses a resizable array
  • Elements are stored in continuous memory locations
  • Fast for accessing elements using index

Example:

ArrayList<String> list = new ArrayList<>();
list.add("Apple");
list.add("Banana");
System.out.println(list.get(1)); // Banana
Enter fullscreen mode Exit fullscreen mode

What is a LinkedList?

A LinkedList is made up of nodes.

  • Each element (node) contains:
    1.Data

    2.Reference to next node (and previous node in doubly linked list)

  • Elements are not stored in continuous memory

Example:

LinkedList<String> list = new LinkedList<>();
list.add("Apple");
list.add("Banana");
System.out.println(list.get(1)); // Banana
Enter fullscreen mode Exit fullscreen mode

Key Differencesa

1. Internal Structure
ArrayList → Uses dynamic array
LinkedList → Uses nodes with pointers

2. Performance

Operation ArrayList LinkedList
Access (get) Fast (O(1)) Slow (O(n))
Insert (middle) Slow Faster
Delete Slow Faster

Why?

  • ArrayList needs shifting elements
  • LinkedList just updates pointers

3. Memory Usage

ArrayList → Less memory (only data)
LinkedList → More memory (data + pointers)

4. When to Use

Use ArrayList when:

  • You need fast access
  • More read operations
  • Less insertion/deletion

Use LinkedList when:

  • You do frequent insert/delete
  • Especially at beginning or middle

Real-Life Analogy

ArrayList → Like a row of seats in a theater
(fixed order, shifting needed)
LinkedList → Like a train
(each coach connected, easy to add/remove)

Final Thoughts

Both ArrayList and LinkedList are useful. The choice depends on your use case:

Want speed in accessing? → Go with ArrayList
Want flexibility in inserting/removing? → Go with

Top comments (0)