Team LiB
Previous Section Next Section

List<E>java.util

Java 1.2collection

This interface represents an ordered collection of objects. In Java 5.0 List is a generic interface and the type variable E represents the type of the objects in the list. Each element in a List has an index, or position, in the list, and elements can be inserted, queried, and removed by index. The first element of a List has an index of 0. The last element in a list has index size( )-1.

In addition to the methods defined by the superinterface, Collection, List defines a number of methods for working with its indexed elements. get( ) and set( ) query and set the object at a particular index, respectively. Versions of add( ) and addAll( ) that take an index argument insert an object or Collection of objects at a specified index. The versions of add( ) and addAll( ) that do not take an index argument insert an object or collection of objects at the end of the list. List defines a version of remove( ) that removes the object at a specified index.

The iterator( ) method is just like the iterator( ) method of Collection, except that the Iterator it returns is guaranteed to enumerate the elements of the List in order. listIterator( ) returns a ListIterator object, which is more powerful than a regular Iterator and allows the list to be modified while iteration proceeds. listIterator( ) can take an index argument to specify where in the list iteration should begin.

indexOf( ) and lastIndexOf( ) perform linear searches from the beginning and end, respectively, of the list, searching for a specified object. Each method returns the index of the first matching object it finds, or -1 if it does not find a match. Finally, subList( ) returns a List that contains only a specified contiguous range of list elements. The returned list is simply a view into the original list, so changes in the original List are visible in the returned List. This subList( ) method is particularly useful if you want to sort, search, clear( ), or otherwise manipulate only a partial range of a larger list.

An interface cannot specify constructors, but it is conventional that all implementations of List provide at least two standard constructors: one that takes no arguments and creates an empty list, and a copy constructor that accepts an arbitrary Collection object that specifies the initial contents of the new List.

As with Collection, List methods that change the contents of the list are optional, and implementations that do not support them simply throw java.lang.UnsupportedOperationException. Different implementations of List may have significantly different efficiency characteristics. For example, the get( ) and set( ) methods of an ArrayList are much more efficient than those of a LinkedList. On the other hand, the add( ) and remove( ) methods of a LinkedList can be more efficient than those of an ArrayList. See also Collection, Set, Map, ArrayList, and LinkedList.

Figure 16-39. java.util.List<E>


public interface List<E> extends Collection<E> {
// Public Instance Methods
     boolean add(E o);  
     void add(int index, E element);  
     boolean addAll(Collection<? extends E> c);  
     boolean addAll(int index, Collection<? extends E> c);  
     void clear( );  
     boolean contains(Object o);  
     boolean containsAll(Collection<?> c);  
     boolean equals(Object o);  
     E get(int index);  
     int hashCode( );  
     int indexOf(Object o);  
     boolean isEmpty( );  
     Iterator<E> iterator( );  
     int lastIndexOf(Object o);  
     ListIterator<E> listIterator( );  
     ListIterator<E> listIterator(int index);  
     boolean remove(Object o);  
     E remove(int index);  
     boolean removeAll(Collection<?> c);  
     boolean retainAll(Collection<?> c);  
     E set(int index, E element);  
     int size( );  
     List<E> subList(int fromIndex, int toIndex);  
     Object[ ] toArray( );  
     <T> T[ ] toArray(T[ ] a);  
}

Implementations

AbstractList, ArrayList, LinkedList, Vector, java.util.concurrent.CopyOnWriteArrayList

Passed To

Too many methods to list.

Returned By

Too many methods to list.

Type Of

Collections.EMPTY_LIST

    Team LiB
    Previous Section Next Section