innych odpowiedzi wyjaśniły naprawdę dobrze, ale tylko do przechowywania rzeczy istotne, w JDK 1.7.0_95:
/**
* Constructs a new {@code ArrayList} instance with zero initial capacity.
*/
public ArrayList() {
array = EmptyArray.OBJECT;
}
/**
* Constructs a new instance of {@code ArrayList} with the specified
* initial capacity.
*
* @param capacity
* the initial capacity of this {@code ArrayList}.
*/
public ArrayList(int capacity) {
if (capacity < 0) {
throw new IllegalArgumentException("capacity < 0: " + capacity);
}
array = (capacity == 0 ? EmptyArray.OBJECT : new Object[capacity]);
}
Jako komentarz wspomina, konstruktor przyjmując żadnych argumentów inicjuje ArrayList z zerowym początkowej pojemności.
A nawet bardziej interesujące tutaj jest zmienna (z komentarzem), który nadaje wiele informacji na własną rękę:
/**
* The minimum amount by which the capacity of an ArrayList will increase.
* This tuning parameter controls a time-space tradeoff. This value (12)
* gives empirically good results and is arguably consistent with the
* RI's specified default initial capacity of 10: instead of 10, we start
* with 0 (sans allocation) and jump to 12.
*/
private static final int MIN_CAPACITY_INCREMENT = 12;
Możesz dodać jedenasty element, ale dopiero po dodaniu pierwszego. 10. – ajb