Java ArrayList Overview and Source Code Walkthrough
This article provides an overview of Java's ArrayList class, explaining its dynamic array implementation, thread-safety considerations, key interfaces, constructors, capacity management, core methods, and internal mechanisms such as ensureCapacity, trimToSize, and serialization, accompanied by detailed source code excerpts and practical insights.
The article introduces ArrayList, a core Java collection implemented as a dynamic array that automatically grows its capacity. It highlights that ArrayList is not thread‑safe by default and can be wrapped with Collections.synchronizedList or replaced by CopyOnWriteArrayList for concurrent use.
It explains that ArrayList implements Serializable, RandomAccess, Cloneable, and the List interface, allowing fast random access, cloning, and serialization of its elements.
The source code of the ArrayList class is presented in full, preserving the original formatting and comments. Key sections include the serialVersionUID, the internal Object[] elementData array, constructors (default capacity 10, capacity‑specified, and collection‑based), and essential methods such as trimToSize, ensureCapacity, add, size, contains, indexOf, lastIndexOf, get, set, remove, clear, addAll, removeRange, clone, and the custom serialization methods writeObject and readObject.
package java.util;
public class ArrayList<E> extends AbstractList<E> implements List<E>, 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<? extends E> c) { ... }
public void trimToSize() { ... }
public void ensureCapacity(int minCapacity) { ... }
public boolean add(E e) { ... }
public int size() { return size; }
public boolean contains(Object o) { return indexOf(o) >= 0; }
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> T[] toArray(T[] a) { ... }
public E get(int index) { RangeCheck(index); return (E) elementData[index]; }
public E set(int index, E element) { RangeCheck(index); E old = (E) elementData[index]; elementData[index] = element; return old; }
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<? extends E> c) { ... }
public boolean addAll(int index, Collection<? extends E> 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 { ... }
}Key takeaways include:
The default constructor creates an ArrayList with an initial capacity of 10; the collection‑based constructor copies the collection into the internal array. ensureCapacity grows the internal array by 1.5× plus one when needed, making bulk insertions expensive unless the final size is known in advance.
Both Arrays.copyOf and System.arraycopy are heavily used for resizing and shifting elements; understanding these methods helps explain performance characteristics. toArray() and toArray(T[] a) provide ways to obtain a plain array, with the generic version handling type conversion internally.
Because ArrayList is array‑backed, random access is fast (O(1)), but insertions and deletions in the middle require element shifting, leading to O(n) cost.
The implementation treats null elements specially, allowing ArrayList to store null values.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
