Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Collections

The ArrayList class

Reference for java.util.ArrayList: constructors, growth strategy, fast indexed access, costs of insertion and removal, and thread-safety caveats.

ArrayList<E> is a List backed by a resizable array. It gives constant-time access by index, amortized constant-time appends and a compact memory layout, which makes it the default choice when you need a list. It allows null elements and duplicates and keeps insertion order.

When the internal array is full, the list allocates a larger one (about 1.5 times the old size) and copies the elements across. If you know roughly how many elements you will add, pass an initial capacity or call ensureCapacity() to avoid repeated copying.

Java
List<String> names = new ArrayList<>();
ArrayList<Integer> numbers = new ArrayList<>(1000);   // initial capacity

Try it

Java Creating and changing an ArrayList

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Common pitfalls

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
Constructor Description
ArrayList() Empty list; the array is allocated on first add.
ArrayList(int initialCapacity) Empty list with room for that many elements. Throws IllegalArgumentException if negative.
ArrayList(Collection<? extends E> c) Copy of the collection, in its iteration order. Throws NullPointerException if c is null.
Member Description
add, addAll, get, set, remove, indexOf, contains, sort, subList All List operations.
void ensureCapacity(int minCapacity) Grows the internal array ahead of time.
void trimToSize() Shrinks the array to the current size.
Object clone() Shallow copy (elements are shared).

Usage notes

  • Complexity: get/set O(1); add(e) amortized O(1); add(i, e) and remove(i) O(n); contains/indexOf O(n).
  • Not synchronized. Wrap with Collections.synchronizedList() or use CopyOnWriteArrayList for shared use. See Concurrent collections.
  • Fail-fast iterators: structural changes during iteration throw ConcurrentModificationException on a best-effort basis.
  • Autoboxing trap: remove(int) versus remove(Object) on List<Integer>.
  • Memory: a list of Integer stores references to boxed objects; for large numeric data consider int[] or a primitive stream.
  • new ArrayList<>(n) is not a size. The list is still empty, so get(0) throws; n is only capacity.
  • Copying: new ArrayList<>(other) is a shallow copy; List.copyOf(other) creates an immutable one.
  • subList views are tied to the parent list; copy them into a new ArrayList if you need independence.