Java Reference
In-Depth Information
21.6.
List
The
List<E>
interface extends
Collection<E>
to define a collection whose
elements have a defined ordereach element exists in a particular position
in the collection, indexed from
0
to
list.size()-1
. Or, in other words, a
List
defines a
sequence
of elements. This requires a refinement of the
contracts of several methods inherited from
Collection
: when you
add
an
element, it is placed at the end of the list; When you
remove
the
n
th
ele-
ment from the list, the element that was after it is shifted over, becoming
the new
n
th
element; and the
toArray
methods fill in the array in the list's
order.
List
also adds several methods that make sense in an ordered collection:
public E
get(int index)
Returns the
index
th
entry in the list.
public E
set(int index, E elem)
Sets the index
th
entry in the list to
elem
, replacing the previous
element and returning it. (Optional)
public void
add(int index, E elem)
Adds the entry
elem
to the list at the
index
th
position, shifting
every element farther in the list down one position. (Optional)
public E
remove(int index)
Removes and returns the
index
th
entry in the list, shifting every
element farther in the list up one position. (Optional)
public int
indexOf(Object elem)