9 for in 0 i forj 0 j aMPos MPos J swapi MPos 10 for i n
Solution
Add
Remove
Get
Contains
Data Structure
ArrayList
O(1)
O(n)
O(1)
O(n)
Array
LinkedList
O(1)
O(1)
O(n)
O(n)
Linked List
CopyonWriteArrayList
O(n)
O(n)
O(1)
O(n)
Array
Operation
Array
ArrayList
Singly Linked List
Read (any where)
O(1)
O(1)
O(n)
Add/Remove at end
O(1)
O(1)
O(n)
Add/Remove in the interior
O(n)
O(n)
O(n)
Resize
O(n)
N/A
N/A
Find By position
O(1)
O(1)
O(n)
Find By target (value)
O(n)
O(n)
O(n)
Array:
1- Excessive read, as time complexity of read is always O(1)
2- Random access to element using index: if you
ArrayList:
1- Excessive read
2- Random access to elements using their index
3- More flexible in coding
4- Effective use of memory space as items get allocated as needed
LinkedList:
1- Effective use of memory space as items get allocated as needed
2- Excessive Add/Remove of elements (It\'s better than ArrayList, because since array list items get stored in memory in adjacent place. when adding a new element in the middle of the array list, all the items after the inserted one have to be shifted, with Linked list the new item gets injected in the list without the need to shift the other items as they are not adjacent in the memory)
The time complexity comparison is as follows:
ArrayList has O(n) time complexity for arbitrary indices of add/remove, but O(1) for the operation at the end of the list.
LinkedList has O(n) time complexity for arbitrary indices of add/remove, but O(1) for operations at end/beginning of the List.
| Add | Remove | Get | Contains | Data Structure | |
| ArrayList | O(1) | O(n) | O(1) | O(n) | Array |
| LinkedList | O(1) | O(1) | O(n) | O(n) | Linked List |
| CopyonWriteArrayList | O(n) | O(n) | O(1) | O(n) | Array |


