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.
List<String> names = new ArrayList<>(); ArrayList<Integer> numbers = new ArrayList<>(1000); // initial capacity
Try it
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.
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
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/setO(1);add(e)amortizedO(1);add(i, e)andremove(i)O(n);contains/indexOfO(n). - Not synchronized. Wrap with
Collections.synchronizedList()or useCopyOnWriteArrayListfor shared use. See Concurrent collections. - Fail-fast iterators: structural changes during iteration throw
ConcurrentModificationExceptionon a best-effort basis. - Autoboxing trap:
remove(int)versusremove(Object)onList<Integer>. - Memory: a list of
Integerstores references to boxed objects; for large numeric data considerint[]or a primitive stream. new ArrayList<>(n)is not a size. The list is still empty, soget(0)throws;nis only capacity.- Copying:
new ArrayList<>(other)is a shallow copy;List.copyOf(other)creates an immutable one. subListviews are tied to the parent list; copy them into a newArrayListif you need independence.