public abstract class AbstractSequentialList4D<E>
extends java.util.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 | Constructor and Description |
---|---|
protected |
AbstractSequentialList4D()
The main constructor, for use by subclasses.
|
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,
java.util.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.
|
java.util.Iterator<E> |
iterator()
Obtain an Iterator over this list, whose sequence is the list order.
|
abstract java.util.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, removeRange, subList
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, size, toArray, toArray, toString
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
protected AbstractSequentialList4D()
public abstract java.util.ListIterator<E> listIterator(int index)
public void add(int index, E o)
add
in interface java.util.List<E>
add
in class java.util.AbstractList<E>
index
- the location to insert the itemo
- the object to insertjava.lang.UnsupportedOperationException
- if this list does not support the
add operationjava.lang.IndexOutOfBoundsException
- if index < 0 || index > size()java.lang.ClassCastException
- if o cannot be added to this list due to its
typejava.lang.IllegalArgumentException
- if o cannot be added to this list for
some other reason.java.lang.NullPointerException
- if o is null and the list does not permit
the addition of null values.public boolean addAll(int index, java.util.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 java.util.List<E>
addAll
in class java.util.AbstractList<E>
index
- the location to insert the collectionc
- the collection to insertjava.lang.UnsupportedOperationException
- if this list does not support the
addAll operationjava.lang.IndexOutOfBoundsException
- if index < 0 || index > size()java.lang.ClassCastException
- if some element of c cannot be added to this
list due to its typejava.lang.IllegalArgumentException
- if some element of c cannot be added
to this list for some other reasonjava.lang.NullPointerException
- if the specified collection is nulljava.lang.NullPointerException
- 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)
public java.util.Iterator<E> iterator()
public E remove(int index)
remove
in interface java.util.List<E>
remove
in class java.util.AbstractList<E>
index
- the position within the list of the object to removejava.lang.UnsupportedOperationException
- if this list does not support the
remove operationjava.lang.IndexOutOfBoundsException
- if index < 0 || index >= size()public E set(int index, E o)
set
in interface java.util.List<E>
set
in class java.util.AbstractList<E>
index
- the position within this list of the element to be replacedo
- the object to replace it withjava.lang.UnsupportedOperationException
- if this list does not support the
set operationjava.lang.IndexOutOfBoundsException
- if index < 0 || index >= size()java.lang.ClassCastException
- if o cannot be added to this list due to its
typejava.lang.IllegalArgumentException
- if o cannot be added to this list for
some other reasonjava.lang.NullPointerException
- if o is null and the list does not allow
a value to be set to null.