public abstract class AbstractSequentialList<E> extends AbstractList<E>
get
, set
, add
, and
remove
) atop the list iterator, opposite of AbstractList's
approach of implementing the iterator atop random access.
To implement a list, you need an implementation for size()
and listIterator
. With just hasNext
,
next
, hasPrevious
, previous
,
nextIndex
, and previousIndex
, you have an
unmodifiable list. For a modifiable one, add set
, and for
a variable-size list, add add
and remove
.
The programmer should provide a no-argument constructor, and one that accepts another Collection, as recommended by the Collection interface. Unfortunately, there is no way to enforce this in Java.
Collection
,
List
,
AbstractList
,
AbstractCollection
,
ListIterator
,
LinkedList
Modifier and Type | Method and Description |
---|---|
void |
add(int index,
E o)
Insert an element into the list at a given position (optional operation).
|
boolean |
addAll(int index,
Collection<? extends E> c)
Insert the contents of a collection into the list at a given position
(optional operation).
|
E |
get(int index)
Get the element at a given index in this list.
|
Iterator<E> |
iterator()
Obtain an Iterator over this list, whose sequence is the list order.
|
abstract ListIterator<E> |
listIterator(int index)
Returns a ListIterator over the list, starting from position index.
|
E |
remove(int index)
Remove the element at a given position in this list (optional operation).
|
E |
set(int index,
E o)
Replace an element of this list with another object (optional operation).
|
add, clear, equals, hashCode, indexOf, lastIndexOf, listIterator, subList
public void add(int index, E o)
add
in interface List<E>
add
in class AbstractList<E>
index
- the location to insert the itemo
- the object to insertUnsupportedOperationException
- if this list does not support the
add operationIndexOutOfBoundsException
- if index < 0 || index > size()ClassCastException
- if o cannot be added to this list due to its
typeIllegalArgumentException
- if o cannot be added to this list for
some other reason.NullPointerException
- if o is null and the list does not permit
the addition of null values.AbstractList.modCount
public boolean addAll(int index, Collection<? extends E> c)
This implementation grabs listIterator(index), then proceeds to use add for each element returned by c's iterator. Sun's online specs are wrong, claiming that this also calls next(): listIterator.add() correctly skips the added element.
addAll
in interface List<E>
addAll
in class AbstractList<E>
index
- the location to insert the collectionc
- the collection to insertUnsupportedOperationException
- if this list does not support the
addAll operationIndexOutOfBoundsException
- if index < 0 || index > size()ClassCastException
- if some element of c cannot be added to this
list due to its typeIllegalArgumentException
- if some element of c cannot be added
to this list for some other reasonNullPointerException
- if the specified collection is nullNullPointerException
- if an object, o, in c is null and the list
does not permit the addition of null values.add(int, Object)
public E get(int index)
get
in interface List<E>
get
in class AbstractList<E>
index
- the index of the element to be returnedIndexOutOfBoundsException
- if index < 0 || index >= size()public Iterator<E> iterator()
iterator
in interface Iterable<E>
iterator
in interface Collection<E>
iterator
in interface List<E>
iterator
in class AbstractList<E>
AbstractList.modCount
public abstract ListIterator<E> listIterator(int index)
listIterator
in interface List<E>
listIterator
in class AbstractList<E>
index
- the starting position of the listIndexOutOfBoundsException
- if index < 0 || index > size()AbstractList.modCount
public E remove(int index)
remove
in interface List<E>
remove
in class AbstractList<E>
index
- the position within the list of the object to removeUnsupportedOperationException
- if this list does not support the
remove operationIndexOutOfBoundsException
- if index < 0 || index >= size()AbstractList.modCount
public E set(int index, E o)
set
in interface List<E>
set
in class AbstractList<E>
index
- the position within this list of the element to be replacedo
- the object to replace it withUnsupportedOperationException
- if this list does not support the
set operationIndexOutOfBoundsException
- if index < 0 || index >= size()ClassCastException
- if o cannot be added to this list due to its
typeIllegalArgumentException
- if o cannot be added to this list for
some other reasonNullPointerException
- if o is null and the list does not allow
a value to be set to null.