arraylist in java and java arraylist methods

Understanding ArrayList in Java

The ArrayList class implements the List interface, so it inherits every behavior defined by that interface. As a member of the collection framework, an ArrayList can hold any number of elements or objects. Think of it as a growable array: unlike a standard array, it has no fixed length, so its size expands or contracts automatically whenever objects are added or removed. Elements are stored by index, just like in an array, and this guide explains how that automatic resizing actually works.

Key Characteristics of ArrayList

  • Index-based storage: every element sits at a specific position, and you can add, retrieve, or remove items using that index value.
  • Duplicates are allowed, because each element is placed at its own index position and the list does not check whether the value already exists.
  • Insertion order is preserved: an element added at index 0 stays reachable at index 0 throughout the program unless you change it.
  • Any number of null values can be stored, unlike collections such as TreeSet that reject nulls.
  • By default the class is non-synchronized, meaning several threads may access it concurrently; you can synchronize it with the synchronized keyword or switch to CopyOnWriteArrayList for multithreaded use.
  • The initial default capacity is 10.
  • ArrayList works only with non-primitive data types; primitives such as int or char cannot be used directly.
  • Random access is supported, so any element can be fetched quickly through its index.

Creating an ArrayList in Java

Because the ArrayList class is generic, you must specify the data type when creating one. Three constructors are available, and each serves a different purpose.

ArrayList() — Default Capacity

The no-argument constructor builds an ArrayList with the default size of 10. For example, ArrayList<String> names = new ArrayList<String>(); creates a list named names that stores String elements. The capacity starts at 10 and grows as elements are added.

ArrayList(int capacity) — Specified Capacity

This parameterized constructor accepts an int value and creates an empty ArrayList with that capacity. If you already know roughly how much data you will store, declaring the capacity up front is a good idea. A negative capacity triggers an IllegalArgumentException.

ArrayList(Collection c) — From Another Collection

This constructor creates an ArrayList containing the elements of the specified collection, with an initial capacity large enough to hold them. It is handy when you want to copy an existing collection into a new list.

Initializing an ArrayList

You can initialize an ArrayList either at the moment of creation or afterward. Using a double-brace block, elements can be added while the list is being constructed, which is a compact way to seed initial values.

How ArrayList Works Internally

When you call the default constructor, the JVM creates the ArrayList in heap memory with the default size of 10. Since the list is a growable array, it has no fixed length and adjusts its size dynamically as elements are added or removed. You can also set the capacity through the constructor — for instance, creating a list with capacity 15 — but the list will still accept more than 15 elements because it expands automatically when needed.

Load Factor and Threshold

Growth is governed by the load factor and the current capacity. The load factor determines when the capacity should increase, and the default value for ArrayList is 0.75f. The threshold is calculated as current capacity multiplied by the load factor. For a list with 10 elements and a load factor of 0.75f, the threshold is 10 × 0.75 = 7. Once that threshold is crossed, the capacity is increased.

Frequently Asked Questions

Does ArrayList have a fixed length?
No. It is a growable array whose size changes automatically as objects are added or removed.
Can an ArrayList hold duplicate or null values?
Yes to both. Duplicates are stored at their own index positions, and any number of null values is permitted.
Is ArrayList synchronized by default?
No. It is non-synchronized by default, so multiple threads can access it at once; use the synchronized keyword or CopyOnWriteArrayList when thread safety is required.
What is the default capacity of an ArrayList?
The initial default capacity is 10, and it grows according to the load factor and threshold.

Have a question or a comment about this article? Leave a reply or head back to the home page to keep exploring.