Algorithms

ArrayList / Dynamic Array

๐Ÿ“‹ Quick Reference

PropertyValue
TypeSequential, indexed collection
AccessO(1) random access by index
Insert/DeleteO(n) - requires shifting elements
AppendO(1) amortized (O(n) when resizing)
MemoryContiguous block, cache-friendly
Best ForRandom access, iteration, append-heavy workloads
One-liner: Dynamic array that grows automatically when full, providing O(1) index access.

๐ŸŽฎ Interactive Visualizer

Watch how ArrayList handles operations including the automatic resize when capacity is exceeded:

Loading visualizer...
Try these operations:
  1. Add elements until resize triggers (capacity doubles)
  2. Insert at index 0 - see all elements shift right
  3. Remove from middle - see elements shift left
  4. Access by index - instant O(1) operation

๐Ÿ”ง Key Operations

Creating

JAVA(11 lines)
Code
Loading syntax highlighter...

Adding Elements

JAVA(3 lines)
Code
Loading syntax highlighter...

Accessing

JAVA(3 lines)
Code
Loading syntax highlighter...

Removing

JAVA(3 lines)
Code
Loading syntax highlighter...

Iteration

JAVA(10 lines)
Code
Loading syntax highlighter...

๐Ÿ“Š Complexity Analysis

OperationTimeNotes
get(i)O(1)Direct array access
set(i, e)O(1)Direct array access
add(e)O(1)**Amortized - O(n) when resize needed
add(i, e)O(n)Shifts elements right
remove(i)O(n)Shifts elements left
contains(e)O(n)Linear search
size()O(1)Stored as field

Resize Strategy

Capacity doubles: 10 โ†’ 20 โ†’ 40 โ†’ 80 โ†’ 160 โ†’ ...

Growth factor = 1.5x (in some implementations)
New capacity = oldCapacity + (oldCapacity >> 1)

โœ… When to Use ArrayList

Good Use Cases

  • Random access needed - get/set by index frequently
  • Mostly appending - add to end is O(1) amortized
  • Iteration-heavy - cache-friendly memory layout
  • Known size - pre-size to avoid resizing

Avoid When

  • Frequent insert/delete at front - use ArrayDeque or LinkedList
  • Frequent insert/delete in middle - consider LinkedList
  • Need thread safety - use CopyOnWriteArrayList or synchronize
  • Primitive types - boxing overhead; use primitive arrays or specialized libs

โš ๏ธ Common Pitfalls

1. ConcurrentModificationException

JAVA(17 lines)
Code
Loading syntax highlighter...

2. Forgetting Initial Capacity

JAVA(8 lines)
Code
Loading syntax highlighter...

3. Sublist Gotcha

JAVA(3 lines)
Code
Loading syntax highlighter...

๐ŸŽค Interview Tips

Q: When would you choose ArrayList over LinkedList?
"

Almost always. ArrayList has O(1) random access, better cache locality, and lower memory overhead. LinkedList only wins for frequent insertions/deletions at known positions with an iterator, which is rare.

Q: What happens when ArrayList is full?
"

It creates a new array ~1.5x larger, copies all elements, then discards the old array. This is O(n) but happens infrequently enough that add() is O(1) amortized.

Q: How would you make ArrayList thread-safe?
"
Options: Collections.synchronizedList(list), CopyOnWriteArrayList (for read-heavy), or external synchronization. ConcurrentHashMap has no direct List equivalent.

๐Ÿ“š Series Navigation


Visualizer: ArrayList from @tomaszjarosz/react-visualizers