|
|
The java.util Package - The List Interface
- a List is a collection whose elements can be accessed by an index
- the indices are zero-based
- a list has methods for inserting and removing elements
- a list can contain duplicate elements
- a List provides a special ListIterator which allows you to move backwards and forwards through the elements
- there are three basic ways in which a List can be modified:
- add an element
- remove an element
- replace an element
- a list can support any or none of the above; attempts to modify a list that does not support the above will result in an UnsupportedOperationException
- there is no way to append Lists unless you provide your own method
| ArrayList |
extends AbstractList implements List, Cloneable, Serializable
Elements are ordered.
Internally uses an array to store elements.
Index access is quick, while adding and removing elements, except at the end of the array, is expensive.
|
| LinkedList |
extends AbstractSequentialList implements List, Cloneable, Serializable
Elements are ordered.
Internally uses a doubly linked list to store elements.
Adding and removing elements involves updating two links; index access is slow as the entire list must be traversed.
LinkedList retains a reference to both the first and last elements; retrieving the first or last element is efficient.
|
| Vector |
extends AbstractList implements List, Cloneable, Serializable
Older class that was modified in JDK 1.2 to implement List.
An expansible array.
The vector will grow automatically to take new objects. You can also shrink a Vector.
Otherwise, manipulated the same as an array.
May contain null elements.
All methods are synchronized
|
| get(int index) |
returns the element at the specified position |
| set(int index, Object element) |
replaces the element at the specified position with the given object |
| add(int index, Object element) |
inserts the specified element at the specified position, shifting all the elements and adds one to their index values |
| remove(int index) |
removes the element at the specified position, shifiting all the elements and subtracting one from their indices |
| indexOf(Object o) |
returns the index of the first occurence of the specified element or -1 if it is not found |
| lastIndexOf(Object o) |
returns the index of the last occurence of the specified element or -1 if it is not found |
| listIterator() |
returns a list iterator of the elements in their proper sequence |
| listIterator(int index) |
returns a list iterator of elements starting at the specified index |
| subList(int fromIndex, int toIndex) |
returns the portion of the list between the specified indices exclusive of the toIndex element |
|