public abstract class AbstractList<E> extends AbstractCollection<E> implements List<E>
AbstractList is an abstract implementation of the List interface, optimized
for a backing store which supports random access. This implementation does
not support adding or replacing. A subclass must implement the abstract
methods get() and size(), and to create a
modifiable List it's necessary to override the add() method that
currently throws an UnsupportedOperationException.| Modifier and Type | Field and Description |
|---|---|
protected int |
modCount
A counter for changes to the list.
|
| Modifier | Constructor and Description |
|---|---|
protected |
AbstractList()
Constructs a new instance of this AbstractList.
|
| Modifier and Type | Method and Description |
|---|---|
boolean |
add(E object)
Adds the specified object at the end of this List.
|
void |
add(int location,
E object)
Inserts the specified object into this List at the specified location.
|
boolean |
addAll(int location,
Collection<? extends E> collection)
Inserts the objects in the specified Collection at the specified location
in this List.
|
void |
clear()
Removes all elements from this list, leaving it empty.
|
boolean |
equals(Object object)
Compares the specified object to this list and return true if they are
equal.
|
abstract E |
get(int location)
Returns the element at the specified location in this list.
|
int |
hashCode()
Returns the hash code of this list.
|
int |
indexOf(Object object)
Searches this list for the specified object and returns the index of the
first occurrence.
|
Iterator<E> |
iterator()
Returns an iterator on the elements of this list.
|
int |
lastIndexOf(Object object)
Searches this list for the specified object and returns the index of the
last occurrence.
|
ListIterator<E> |
listIterator()
Returns a ListIterator on the elements of this list.
|
ListIterator<E> |
listIterator(int location)
Returns a list iterator on the elements of this list.
|
E |
remove(int location)
Removes the object at the specified location from this list.
|
protected void |
removeRange(int start,
int end)
Removes the objects in the specified range from the start to the end
index minus one.
|
E |
set(int location,
E object)
Replaces the element at the specified location in this list with the
specified object.
|
List<E> |
subList(int start,
int end)
Returns a part of consecutive elements of this list as a view.
|
Object[] |
toArray()
Returns a new array containing all elements contained in this
ArrayList. |
<T> T[] |
toArray(T[] contents)
Returns an array containing all elements contained in this
ArrayList. |
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, size, toStringprotected AbstractList()
public void add(int location,
E object)
Concrete implementations that would like to support the add functionality must override this method.
add in interface List<E>location - the index at which to insert.object - the object to add.UnsupportedOperationException - if adding to this List is not supported.ClassCastException - if the class of the object is inappropriate for this
ListIllegalArgumentException - if the object cannot be added to this ListIndexOutOfBoundsException - if location < 0 || >= size()public boolean add(E object)
add in interface Collection<E>add in interface List<E>add in class AbstractCollection<E>object - the object to addUnsupportedOperationException - if adding to this List is not supportedClassCastException - if the class of the object is inappropriate for this
ListIllegalArgumentException - if the object cannot be added to this Listpublic boolean addAll(int location,
Collection<? extends E> collection)
addAll in interface List<E>location - the index at which to insert.collection - the Collection of objectstrue if this List is modified, false otherwise.UnsupportedOperationException - if adding to this list is not supported.ClassCastException - if the class of an object is inappropriate for this list.IllegalArgumentException - if an object cannot be added to this list.IndexOutOfBoundsException - if location < 0 || > size()public void clear()
clear in interface Collection<E>clear in interface List<E>clear in class AbstractCollection<E>UnsupportedOperationException - if removing from this list is not supported.List.isEmpty(),
List.size()public boolean equals(Object object)
equals in interface Collection<E>equals in interface List<E>equals in class Objectobject - the object to compare to this object.true if the specified object is equal to this list,
false otherwise.hashCode()public abstract E get(int location)
get in interface List<E>location - the index of the element to return.IndexOutOfBoundsException - if location < 0 || >= size()public int hashCode()
hashCode in interface Collection<E>hashCode in interface List<E>hashCode in class Objectequals(java.lang.Object),
List.hashCode()public int indexOf(Object object)
public Iterator<E> iterator()
public int lastIndexOf(Object object)
lastIndexOf in interface List<E>object - the object to search for.public ListIterator<E> listIterator()
listIterator in interface List<E>ListIteratorpublic ListIterator<E> listIterator(int location)
listIterator in interface List<E>location - the index at which to start the iteration.IndexOutOfBoundsException - if location < 0 || location > size()ListIteratorpublic E remove(int location)
remove in interface List<E>location - the index of the object to remove.UnsupportedOperationException - if removing from this list is not supported.IndexOutOfBoundsException - if location < 0 || >= size()protected void removeRange(int start,
int end)
start - the index at which to start removing.end - the index after the last element to remove.UnsupportedOperationException - if removing from this list is not supported.IndexOutOfBoundsException - if start < 0 or start >= size().public E set(int location, E object)
set in interface List<E>location - the index at which to put the specified object.object - the object to add.UnsupportedOperationException - if replacing elements in this list is not supported.ClassCastException - if the class of an object is inappropriate for this list.IllegalArgumentException - if an object cannot be added to this list.IndexOutOfBoundsException - if location < 0 || >= size()public List<E> subList(int start, int end)
This method can be used as a handy method to do some operations on a sub
range of the original list, for example
list.subList(from, to).clear();
If the original list is modified in other ways than through the returned subList, the behavior of the returned subList becomes undefined.
The returned subList is a subclass of AbstractList. The subclass stores offset, size of itself, and modCount of the original list. If the original list implements RandomAccess interface, the returned subList also implements RandomAccess interface.
The subList's set(int, Object), get(int), add(int, Object), remove(int), addAll(int, Collection) and removeRange(int, int) methods first check the bounds, adjust offsets and then call the corresponding methods of the original AbstractList. addAll(Collection c) method of the returned subList calls the original addAll(offset + size, c).
The listIterator(int) method of the subList wraps the original list iterator. The iterator() method of the subList invokes the original listIterator() method, and the size() method merely returns the size of the subList.
All methods will throw a ConcurrentModificationException if the modCount of the original list is not equal to the expected value.
subList in interface List<E>start - start index of the subList (inclusive).end - end index of the subList, (exclusive).start
(inclusive), and ending with end (exclusive)IndexOutOfBoundsException - if (start < 0 || end > size())IllegalArgumentException - if (start > end)public Object[] toArray()
ArrayList.toArray in interface Collection<E>toArray in interface List<E>toArray in class AbstractCollection<E>ArrayListpublic <T> T[] toArray(T[] contents)
ArrayList. If the specified array is large enough to hold the
elements, the specified array is used, otherwise an array of the same
type is created. If the specified array is used and is larger than this
ArrayList, the array element following the collection elements
is set to null.toArray in interface Collection<E>toArray in interface List<E>toArray in class AbstractCollection<E>contents - the array.ArrayList.ArrayStoreException - when the type of an element in this ArrayList cannot
be stored in the type of the specified array.