Home Java ArrayList Methods ensureCapacity() Java ArrayList ensureCapacity() Method Definition In Java, ensureCapacity(minCapacity) asks an ArrayList to make its internal array big enough for at least minCapacity elements. It does not add any elements: the size stays the same.
An ArrayList grows automatically, copying its elements into a bigger array whenever it runs out of room. If you know you are about to add many elements, reserving the room once can save several of those copies. It is only a performance hint; your code works the same without it.
An instance method of java.util.ArrayList (Java 1.2+): a resizable-array list with index access that allows null and duplicates and is not thread-safe .
Syntax Call it on a variable declared as ArrayList.
ArrayList<String> list = new ArrayList<>();
list.ensureCapacity(10000); // room for 10000 elements
// list.size() is still 0 Call What it does list.ensureCapacity(n)Makes room for at least n elements. new ArrayList<>(n)Sets the initial capacity when creating the list. list.trimToSize()The opposite: releases unused room. list.size()Unchanged by ensureCapacity.
ensureCapacity is defined in ArrayList, not in List. If your variable is declared as List<T>, use the new ArrayList<>(n) constructor instead.
Library declarations // java.util.ArrayList<E>
public void ensureCapacity(int minCapacity) Parameters Parameter Description minCapacity (int)The number of elements the list should be able to hold without growing.
Return Value Return Value Description voidNothing; only internal storage may change.
Important
If the current capacity is already large enough, or minCapacity is zero or negative, the call does nothing. There is no method to read the capacity, and the default capacity (10) and the growth rate (about 1.5 times) are implementation details of the JDK, not guarantees. Asking for a huge capacity can throw OutOfMemoryError.
Compare with Similar Methods Capacity and size methods:
Method How it differs trimToSize()Shrinks the capacity to the current size. size()The number of elements, which ensureCapacity never changes. addAll(c)Grows the list once for the whole batch. isEmpty()Still true after reserving room in an empty list.
Java Examples 1. Room is not size 2. Before a big batch 3. Small values Example 1 — Reserve Room Without Adding Elements Reserve space for 500 names and see that the list is still empty.
import java.util.ArrayList;
public class ArrayListEnsureCapacityDemo {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<>();
names.ensureCapacity(500);
System.out.println("size after ensureCapacity(500): " + names.size());
System.out.println("isEmpty: " + names.isEmpty());
try {
names.get(0);
} catch (IndexOutOfBoundsException e) {
System.out.println("get(0) still fails: no elements yet");
}
}
} size after ensureCapacity(500): 0
isEmpty: true
get(0) still fails: no elements yet How It Works ArrayList type. The variable is declared as ArrayList because ensureCapacity is not in the List interface.Only storage. The call may enlarge the internal array, but no elements are created, so the size stays 0.Still empty. isEmpty() looks at the size, so it is still true.No index 0. Reserved slots are not elements; get(0) throws IndexOutOfBoundsException.Example 2 — Reserve Once Before a Large Loop Reserve room for 1000 squares, then add them in a loop.
import java.util.ArrayList;
public class ArrayListEnsureCapacityBatch {
public static void main(String[] args) {
int count = 1000;
ArrayList<Integer> squares = new ArrayList<>();
squares.ensureCapacity(count);
for (int i = 1; i <= count; i++) {
squares.add(i * i);
}
System.out.println("size: " + squares.size());
System.out.println("first: " + squares.get(0) + ", last: " + squares.get(count - 1));
}
} size: 1000
first: 1, last: 1000000 How It Works Known size. The loop will add exactly count elements, so that number is reserved up front.No regrowth. With room for 1000 already in place, none of the adds needs to copy the elements into a bigger array.Same result. Without ensureCapacity the output would be identical; the hint only affects speed and memory churn.The values. The first square is 1 * 1 = 1 and the last is 1000 * 1000 = 1000000, at index 999.Example 3 — Small or Negative Values Do Nothing Call it with values the list already satisfies, then with a larger one.
import java.util.ArrayList;
import java.util.Arrays;
public class ArrayListEnsureCapacityNoop {
public static void main(String[] args) {
ArrayList<String> letters = new ArrayList<>(Arrays.asList("a", "b", "c"));
letters.ensureCapacity(1);
letters.ensureCapacity(0);
letters.ensureCapacity(-5);
System.out.println("after small values: " + letters + " size=" + letters.size());
letters.ensureCapacity(50);
letters.add("d");
System.out.println("after ensureCapacity(50): " + letters + " size=" + letters.size());
}
} after small values: [a, b, c] size=3
after ensureCapacity(50): [a, b, c, d] size=4 How It Works Already enough room. The list can already hold three elements, so asking for 1 or 0 changes nothing.No error for negatives. ensureCapacity(-5) is silently ignored instead of throwing.Bigger request. ensureCapacity(50) may move the three elements into a larger array, but their order and values stay the same.Normal adds. Adding "d" works as usual; capacity changes are invisible apart from performance.Frequently Asked Questions What does ArrayList ensureCapacity() do? It makes the internal array large enough for at least the given number of elements, so later adds do not need to grow it. The size does not change.
Does ensureCapacity() add elements? No. The size stays the same and get(0) on an empty list still throws IndexOutOfBoundsException.
When should I call ensureCapacity()? Right before adding a large, known number of elements to an existing list. For a new list, new ArrayList<>(n) does the same job.
Can I read the capacity of an ArrayList? No. ArrayList has no public method that returns its capacity.
Why can I not call ensureCapacity() on a List variable? It is declared only in ArrayList. Declare the variable as ArrayList, or pass the capacity to the constructor.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
14 people found this page helpful
Helpful Share Copy link Suggestion