Académique Documents
Professionnel Documents
Culture Documents
A List is an ordered Collection (sometimes called a sequence). Lists may contain duplicate elements.
In addition to the operations inherited from Collection, the List interface includes operations for the
following:
Positionalaccess manipulates elements based on their numerical position in the list. This
includes methods such as get, set, add, addAll, and remove.
Search searches for a specified object in the list and returns its numerical position. Search
methods include indexOf and lastIndexOf.
Iteration extends Iterator semantics to take advantage of the list's sequential nature.
ThelistIterator methods provide this behavior.
Rangeview The sublist method performs arbitrary range operations on the list.
The Java platform contains two general-purpose List implementations. ArrayList, which is usually the
better-performing implementation, and LinkedList which offers better performance under certain
circumstances.
Collection Operations
The operations inherited from Collection all do about what you'd expect them to do, assuming you're
already familiar with them. If you're not familiar with them from Collection, now would be a good time to
read The Collection Interface section. The remove operation always removes the first occurrence of the
specified element from the list. The add and addAll operations always append the new element(s) to
the end of the list. Thus, the following idiom concatenates one list to another.
list1.addAll(list2);
Here's a nondestructive form of this idiom, which produces a third List consisting of the second list
appended to the first.
The basic positionalaccess operations are get, set, add and remove.
(The set and remove operations return the old value that is being overwritten or removed.) Other
operations (indexOf and lastIndexOf) return the first or last index of the specified element in the list.
The addAll operation inserts all the elements of the specified Collection starting at the specified
position. The elements are inserted in the order they are returned by the specified Collection's iterator.
This call is the positional access analog of Collection's addAll operation.
Here's a little method to swap two indexed values in a List.