Time Complexity for Java ArrayList

dvanaria picture dvanaria · Jun 30, 2011 · Viewed 58.8k times · Source

I found other entries for this question that dealt with specific methods, but nothing comprehensive. I'd like to verify my own understanding of the most often used methods of this data structure:

O(1) - Constant Time:

isEmpty()
add(x)
add(x, i)
set(x, i)
size()
get(i)
remove(i)

O(N) - Linear Time:

indexof(x)
clear()
remove(x)
remove(i)

Is this correct? Thanks for your help.

Answer

brian_d picture brian_d · Jun 30, 2011

The best resource is straight from the official API:

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.