The capacity of an ArrayList
is the number of elements in the array used to store list elements.
The size of an ArrayList
is the number of elements in the list.
You can only add()
elements at indexes ranging from zero to size
, so if the current size is zero, you can only specify an index of zero.
However, the array that backs the list will not have to be replaced until the size exceeds the original capacity.
Collections objects enforce contracts to maintain invariants for the state of the collection. In this case, lists ensure that an element at an index can be read only if it was explicitly added to the list. If you could skip index 0 and add an element at index 1, what should happen if you get index 0? Return null
? Throw NoSuchElementException
? A primary difference between an ArrayList
and an Object[]
is that the list constrains use of the array to provide well-defined behavior.