public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E>
List
interface to minimize the effort required to implement this interface
backed by a "random access" data store (such as an array). For sequential
access data (such as a linked list), AbstractSequentialList
should
be used in preference to this class.
To implement an unmodifiable list, the programmer needs only to extend
this class and provide implementations for the get(int)
and
size()
methods.
To implement a modifiable list, the programmer must additionally
override the set(int, E)
method (which otherwise
throws an UnsupportedOperationException
). If the list is
variable-size the programmer must additionally override the
add(int, E)
and remove(int)
methods.
The programmer should generally provide a void (no argument) and collection
constructor, as per the recommendation in the Collection
interface
specification.
Unlike the other abstract collection implementations, the programmer does
not have to provide an iterator implementation; the iterator and
list iterator are implemented by this class, on top of the "random access"
methods:
get(int)
,
set(int, E)
,
add(int, E)
and
remove(int)
.
The documentation for each non-abstract method in this class describes its implementation in detail. Each of these methods may be overridden if the collection being implemented admits a more efficient implementation.
This class is a member of the Java Collections Framework.
Modifier and Type | Field | Description |
---|---|---|
protected int |
modCount |
The number of times this list has been structurally modified.
|
Modifier | Constructor | Description |
---|---|---|
protected |
AbstractList() |
Sole constructor.
|
Modifier and Type | Method | Description |
---|---|---|
boolean |
add(E e) |
Appends the specified element to the end of this list (optional
operation).
|
void |
add(int index,
E element) |
Inserts the specified element at the specified position in this list
(optional operation).
|
boolean |
addAll(int index,
Collection<? extends E> c) |
Inserts all of the elements in the specified collection into this
list at the specified position (optional operation).
|
void |
clear() |
Removes all of the elements from this list (optional operation).
|
boolean |
equals(Object o) |
Compares the specified object with this list for equality.
|
abstract E |
get(int index) |
Returns the element at the specified position in this list.
|
int |
hashCode() |
Returns the hash code value for this list.
|
int |
indexOf(Object o) |
Returns the index of the first occurrence of the specified element
in this list, or -1 if this list does not contain the element.
|
Iterator<E> |
iterator() |
Returns an iterator over the elements in this list in proper sequence.
|
int |
lastIndexOf(Object o) |
Returns the index of the last occurrence of the specified element
in this list, or -1 if this list does not contain the element.
|
ListIterator<E> |
listIterator() |
Returns a list iterator over the elements in this list (in proper
sequence).
|
ListIterator<E> |
listIterator(int index) |
Returns a list iterator over the elements in this list (in proper
sequence), starting at the specified position in the list.
|
E |
remove(int index) |
Removes the element at the specified position in this list (optional
operation).
|
protected void |
removeRange(int fromIndex,
int toIndex) |
Removes from this list all of the elements whose index is between
fromIndex , inclusive, and toIndex , exclusive. |
E |
set(int index,
E element) |
Replaces the element at the specified position in this list with the
specified element (optional operation).
|
List<E> |
subList(int fromIndex,
int toIndex) |
Returns a view of the portion of this list between the specified
fromIndex, inclusive, and toIndex, exclusive.
|
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, size, toArray, toArray, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
addAll, contains, containsAll, isEmpty, remove, removeAll, replaceAll, retainAll, size, sort, spliterator, toArray, toArray
parallelStream, removeIf, stream
protected transient int modCount
This field is used by the iterator and list iterator implementation
returned by the iterator
and listIterator
methods.
If the value of this field changes unexpectedly, the iterator (or list
iterator) will throw a ConcurrentModificationException
in
response to the next
, remove
, previous
,
set
or add
operations. This provides
fail-fast behavior, rather than non-deterministic behavior in
the face of concurrent modification during iteration.
Use of this field by subclasses is optional. If a subclass
wishes to provide fail-fast iterators (and list iterators), then it
merely has to increment this field in its add(int, E)
and
remove(int)
methods (and any other methods that it overrides
that result in structural modifications to the list). A single call to
add(int, E)
or remove(int)
must add no more than
one to this field, or the iterators (and list iterators) will throw
bogus ConcurrentModificationExceptions
. If an implementation
does not wish to provide fail-fast iterators, this field may be
ignored.
protected AbstractList()
public boolean add(E e)
Lists that support this operation may place limitations on what elements may be added to this list. In particular, some lists will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. List classes should clearly specify in their documentation any restrictions on what elements may be added.
This implementation calls add(size(), e)
.
Note that this implementation throws an
UnsupportedOperationException
unless
add(int, E)
is overridden.