Java Quick Reference
  Language Fundamentals
  Operators and Assignments
  Flow Control and Exceptions
  Declarations and Access Control
  Garbage Collection
  Overloading and Overriding
  Threads
  The java.lang Package
  The java.util Package
  The java.awt Package
  The java.io Package
  References
  Miscellaneous Notes
  Tips & Traps
  Mock Exams

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:
    1. add an element
    2. remove an element
    3. 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
java.util Implementations of List
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

 

List Methods
Positional Methods
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
Search Methods
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
List Iterator
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

Collections Framework Collection Abstract Classes Iterator List