The List Interface

public interface List<E> extends Collection<E>
    // access by position
    E get (int index); // returns element at specified position
    E set(int index, E o); //replaces element a specified index with o
        // returns over-written element
    void add(int i, E o); //add o at position i
    E remove(int i); // removes element at index i and returns it
    boolean addAll(int i, Collection<? extends E> c); // adds all
        // elements in c to this list, starting at position i
        // Elements are inserted in order they are returned by
        // collection c's iterator

    // Search and find methods
    int indexOf(Object o); // returns index of first occurrence of o, or
        // -1 if o does not appear in list
    int lastIndexOf(Object o); // returns index of last occurrence of o

    // fancy iteration
    ListIterator<E> listIterator(); // returns a ListIterator for this list
    ListIterator<E> listIterator(int i); // returns a ListIterator for
        // this list, starting at position i

    // Operations on a sub-list
    List subList(int from, int to); // returns a view of the sub-list with beginning index from and ending index to-1


JDK contains two List implementation classes:

Example: Write a method that interchanges the position of two elements in a list.
public static void swap(List<String> a, int i, int j)

Operations Inherited from Collection - How Do They Work?

List<String> l1 = new ArrayList<String>();
List<String> l2 = new ArrayList<String>();
l2.addAll(l1); // adds all elements of l1 to l2

List Iterators

public interface ListIterator<E> extends Iterator<E>
    boolean hasPrevious(); // returns true if there is an element
            // in front of the listIterator's implicit cursor
    E previous(); // returns element before iterator's cursor

    int nextIndex(); // returns index of element that would be
         //returned by a call to next()
    int previousIndex(); // returns index of element that would be
        //returned by a call to previous()

    void set(E obj); // replaces last element returned by a call to
        // next() or previous() by obj

    void add(E obj); // adds obj to list right before element that
        // would be returned by next() and after element that would
        // be returned by previous() (if any)
        // obj added right before the implicit cursor

There are two versions of the listIterator() method:

Note: In a list of n elements, there are n+1 valid values for the position of the list iterator: 0 to n. Remember that intuitively speaking, the iterator's cursor is always between 2 elements, the one that would be returned by previous() and the one that would be returned by next().

Example: Assume that myList is a List of Strings. Print the elements in myList in reverse order using a ListIterator.

Modifying a List with an Iterator

Iterator contains the remove() method: removes the last element returned by next().
ListIterator contains set() and add():

Example: Write a method called replace() that replaces all occurrences of one String in a list with another String:
public static void replace(List<String> l, String str, String newStr)
    // put your code here


Operations on a Sub-List

Example: Remove elements in positions start to finish from a list myList:
myList.subList(start, finish+1).clear();

Example: Given a list myList of Strings, find the index of the first occurrence of "hello" in positions 5 to 10 (you can assume that these positions are valid for myList).

The Collections Class


List<String> list = new ArrayList<String>();

Object Ordering

Since our list in the last example contains Strings, the list will be sorted in alphabetical order. If it contained objects of type Date, it would be sorted in chronological order. This is because both the String class and the Date class implement the Comparable interface.

The Comparable interface provides a natural ordering for a class, and allows objects of that class type to be sorted automatically.

Some classes that implement Comparable: Character, Integer, Short, Double, String, Date, etc.

If we call Collections.sort() on a list with elements of a type that does not implement Comparable, Collections.sort() will throw a ClassCastException.

The Comparable Interface

public interface Comparable<T>
    int compareTo(T obj);

The compareTo() method returns a negative int, 0 or a positive int, depending on whether this object is less than, equal to, or greater than obj.

Example: Write a class that represents a person's name. Your class should implement the Comparable interface.