Java Reference
In-Depth Information
A
List
provides the following additional features over a generic collection:
It provides access to its elements using indexes. You can use its
•
add(int index, E element)
,
addAll(int index, Collection<? extends E> c)
,
get(int index)
,
remove(int index)
and
set(int index, E element)
methods to add, get, remove, and replace its elements using indexes.
You can search for the position of an element in the
•
List
using
indexOf(Object o)
or
lastIndexOf(Object o)
methods. The
indexOf()
method searches for the specified object in
the
List
from the beginning and it returns the index of the first occurrence of the object. The
lastIndexOf()
method does the same starting from the end of the list. Both methods return
-1 if the
List
does not contain the specified object.
It provides a method called
•
subList(int fromIndex, int toIndex)
that gives you a sublist of
the original list starting at index
fromIndex
(inclusive) to index
toIndex
(exclusive).
It provides a specialized iterator for its elements, which is an instance of the
ListIterator
interface. This iterator lets you iterate over its elements in both directions (forward
and backward) at the same time. You can get the
ListIterator
for a
List
using its
listIterator()
method. Note that the
Iterator
returned from the
iterator()
method of the
Collection
interface returns a forward-only iterator.
The following are two of many implementation classes for the
List
interface:
•
•
ArrayList
LinkedList
An
ArrayList
is backed up by an array. A
LinkedList
is backed up by a linked list. An
ArrayList
performs better
if you access (get and set) the elements of the list frequently. Accessing elements in an
ArrayList
is faster because the
index of an element becomes the index in the backing array, and accessing an element from an array is always fast.
Adding or removing elements from a list backed by an
ArrayList
performs slower, unless done from the end, because
an
ArrayList
has to perform an array copy internally to keep the elements in sequence. The
LinkedList
performs
better as compared to
ArrayList
for adding and removing elements from the middle of the list. However, it is slower
for accessing elements of the list, unless at the head of the list.
You can create and add some elements to a list as follows:
•
// Create a list of strings
List<String> nameList = new ArrayList<>();
nameList.add("John"); // Adds John at the index 0
nameList.add("Richard"); // Adds Richard at the index 1
The
add(E element)
method of the
List
interface appends the element to the end of the
List
. The
remove(Object o)
method of
List
removes the first occurrence of the element from the beginning of the list.
You can also add elements to a
List
using positional indexes. Note that the index that you use to access any
element must be between 0 and
size
, where
size
is the size of the
List
. You can use
add(int index, E element)
method to insert the specified
element
at the specified
index
. For example,
nameList.add(1, "Sara")
will insert
"Sara"
at index 1, which is the second element in the
List
. When you use an index to add an element to a
List
, the
element at the specified index and elements to the right of the specified index are shifted to the right and their indexes
are incremented by 1. Suppose you have a
List
as shown in Figure
12-3
and you execute the following code:
// Add an element at index 1
nameList.add(1, "Sara");