Time complexity for java ArrayList
An ArrayList
in Java is a List
that is backed by an array
.
The get(index)
method is a constant time, O(1)
, operation.
The code straight out of the Java library for ArrayList.get(index)
:
public E get(int index) {
RangeCheck(index);
return (E) elementData[index];
}
Basically, it just returns a value straight out of the backing array. (RangeCheck(index)
) is also constant time)
It's implementation is done with an array and the get operation is O(1).
javadoc says:
The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation.