Fundamentals 13 min read

Introduction and Source Code Analysis of Java ArrayList

This article introduces Java's ArrayList as a dynamic array implementation, explains its non‑thread‑safe nature, details its constructors, capacity‑growth strategy, key methods such as ensureCapacity, add, remove, toArray, and examines the underlying source code including array copying utilities.

Java Captain
Java Captain
Java Captain
Introduction and Source Code Analysis of Java ArrayList

ArrayList is a dynamic array based on a plain Object[] that automatically grows its capacity, similar to C's dynamic memory allocation, but it is not thread‑safe and should be used in single‑threaded contexts or wrapped with synchronized utilities.

The class implements Serializable, RandomAccess, Cloneable and provides methods for size management, element access, insertion, deletion, and bulk operations, all of which rely heavily on Arrays.copyOf and System.arraycopy for efficient array manipulation.

Key constructors include a no‑arg constructor with default capacity 10, a constructor accepting an initial capacity, and one that builds the list from an existing Collection, converting it to an internal array.

package java.util;
public class ArrayList
extends AbstractList
implements List
, RandomAccess, Cloneable, java.io.Serializable {
    private static final long serialVersionUID = 8683452581122892189L;
    private transient Object[] elementData;
    private int size;
    public ArrayList(int initialCapacity) { ... }
    public ArrayList() { this(10); }
    public ArrayList(Collection
c) { ... }
    public void trimToSize() { ... }
    public void ensureCapacity(int minCapacity) { ... }
    public boolean add(E e) { ... }
    public int size() { return size; }
    public boolean contains(Object o) { ... }
    public boolean isEmpty() { return size == 0; }
    public int indexOf(Object o) { ... }
    public int lastIndexOf(Object o) { ... }
    public Object[] toArray() { return Arrays.copyOf(elementData, size); }
    public
T[] toArray(T[] a) { ... }
    public E get(int index) { ... }
    public E set(int index, E element) { ... }
    public void add(int index, E element) { ... }
    public E remove(int index) { ... }
    public boolean remove(Object o) { ... }
    private void fastRemove(int index) { ... }
    public void clear() { ... }
    public boolean addAll(Collection
c) { ... }
    public boolean addAll(int index, Collection
c) { ... }
    protected void removeRange(int fromIndex, int toIndex) { ... }
    private void RangeCheck(int index) { ... }
    public Object clone() { ... }
    private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException { ... }
    private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { ... }
}

The ensureCapacity method grows the internal array by 1.5× plus one when needed, and falls back to the requested minimum capacity if that is still insufficient, highlighting the cost of frequent resizing.

public static
T[] copyOf(T[] original, int newLength) {
    return (T[]) copyOf(original, newLength, original.getClass());
}
public static
T[] copyOf(U[] original, int newLength, Class
newType) {
    T[] copy = (newType == Object[].class) ? (T[]) new Object[newLength]
            : (T[]) java.lang.reflect.Array.newInstance(newType.getComponentType(), newLength);
    System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
    return copy;
}

ArrayList's toArray methods illustrate how generic arrays are created and populated, with the overloaded version handling cases where the provided array is too small by allocating a new one via Arrays.copyOf .

public static Integer[] vectorToArray2(ArrayList
v) {
    Integer[] newText = (Integer[]) v.toArray(new Integer[0]);
    return newText;
}

Overall, ArrayList offers fast random access due to its array backing but incurs higher costs for insertions and deletions because elements must be shifted, and it permits null elements, handling them specially in search operations.

JavaperformancegenericsDataStructureCollectionsArrayListSourceCode
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.