Lists in Java
Created: March 27, 2019 by [lek-tin]
Last updated: March 27, 2019
List interface:
The List interface specifies two remove methods:
List<Integer> list = new List<>();
// Remove the element at index 4
remove(int index): list.remove(4);
// Remove the first occurrence of the object 4
remove(Object o): list.remove(new Integer(4));
Linked List:
Pros | Cons |
---|---|
Can insert or remove anywhere in the list without shifting elements | Not sortable |
Insert: O(1) | Elements are not stored in contiguous memory addresses |
Remove: O(1) | Find: O(n) , as must traverse to find an element |