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, ...

Get Java in a Nutshell, 5th Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.