-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathArrayList methods
More file actions
22 lines (20 loc) · 1.21 KB
/
ArrayList methods
File metadata and controls
22 lines (20 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Question Time Complexity of class ArrayList methods time complexity
***************************************************************************************************
Source: http://stackoverflow.com/questions/6540511/time-complexity-for-java-arraylist
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. O(n) because,
once the array is full(i.e. ArrayList is full), the array is copied to different location which has high memory,
this copying of all the elements due to insufficient space causes (n) operation. Hence on an average,
the time complexity is O(n), but if this case is avoided then the time complexity is O(1), similar to array addition
operation.
All of the other operations run in linear time [i.e. O(n) time](roughly speaking).
ArrayList methods time complexity:
Read/Search any element O(n). If you know the index then the complexity is O(1)
Update : O(n)
Delete at beginning: O(n)
Delete in middle: O(n)
Delete at end: O(n)
Add at beginning: O(n)
Add in middle: O(n)
Add at end: O(n)
Source: http://www.javaexperience.com/time-complexity-of-collection-classes/#ixzz3QWblGwzS