public abstract class AbstractList<E>
- Object
- AbstractCollection<E>
- AbstractList
ImplementsCollection<E>, Iterable<E>, List<E>
Known subtypesAbstractSequentialList, ArrayList, Vector
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.Fields
protected transient int modCount | A counter for changes to the list. |
Constructors
protected AbstractList() | Constructs a new instance of this AbstractList. |
Methods
public void add(int location, E object) | Inserts the specified object into this List at the specified location. |
public boolean add(E object) | Adds the specified object at the end of this List. |
public boolean addAll(int location, Collection<? extends E> collection) | Inserts the objects in the specified Collection at the specified location in this List. |
public void clear() | Removes all elements from this list, leaving it empty. |
public boolean equals(Object object) | Compares the specified object to this list and return true if they are equal. |
public abstract E get(int location) | Returns the element at the specified location in this list. |
public int hashCode() | Returns the hash code of this list. |
public int indexOf(Object object) | Searches this list for the specified object and returns the index of the first occurrence. |
public Iterator<E> iterator() | Returns an iterator on the elements of this list. |
public int lastIndexOf(Object object) | Searches this list for the specified object and returns the index of the last occurrence. |
public ListIterator<E> listIterator() | Returns a ListIterator on the elements of this list. |
public ListIterator<E> listIterator(int location) | Returns a list iterator on the elements of this list. |
public 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. |
public E set(int location, E object) | Replaces the element at the specified location in this list with the specified object. |
public List<E> subList(int start, int end) | Returns a part of consecutive elements of this list as a view. |
public Object[] toArray() | Returns a new array containing all elements contained in this ArrayList. |
public <T> T[] toArray(T[] contents) | Returns an array containing all elements contained in this ArrayList. |
Inherited methods
From AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, size, toString
From Collection
From List
Field details
modCount
protected transient int modCountConstructor details
AbstractList
protected AbstractList()Method details
add
public void add(int location, E object)Inserts the specified object into this List at the specified location. The object is inserted before any previous element at the specified location. If the location is equal to the size of this List, the object is added at the end.
Concrete implementations that would like to support the add functionality must override this method.
Parameters
locationint- the index at which to insert.
objectE- the object to add.
Throws
UnsupportedOperationException- if adding to this List is not supported.
ClassCastException- if the class of the object is inappropriate for this List
IllegalArgumentException- if the object cannot be added to this List
IndexOutOfBoundsException- if
location = size()
add
public boolean add(E object)Parameters
objectE- the object to add
Returns
Throws
UnsupportedOperationException- if adding to this List is not supported
ClassCastException- if the class of the object is inappropriate for this List
IllegalArgumentException- if the object cannot be added to this List
addAll
public boolean addAll(int location, Collection<? extends E> collection)Parameters
locationint- the index at which to insert.
collectionCollection<? extends E>- the Collection of objects
Returns
true if this List is modified, false otherwise.Throws
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 size()
clear
public void clear()Throws
UnsupportedOperationException- if removing from this list is not supported.
See also
equals
public boolean equals(Object object)Parameters
objectObject- the object to compare to this object.
Returns
true if the specified object is equal to this list,
false otherwise.See also
get
public abstract E get(int location)Parameters
locationint- the index of the element to return.
Returns
Throws
IndexOutOfBoundsException- if
location = size()
hashCode
public int hashCode()Returns
See also
indexOf
public int indexOf(Object object)Parameters
objectObject- the object to search for.
Returns
iterator
public Iterator<E> iterator()Returns
See also
lastIndexOf
public int lastIndexOf(Object object)Parameters
objectObject- the object to search for.
Returns
listIterator
public ListIterator<E> listIterator()Returns
See also
listIterator
public ListIterator<E> listIterator(int location)Parameters
locationint- the index at which to start the iteration.
Returns
Throws
IndexOutOfBoundsException- if
location size()
See also
remove
public E remove(int location)Parameters
locationint- the index of the object to remove.
Returns
Throws
UnsupportedOperationException- if removing from this list is not supported.
IndexOutOfBoundsException- if
location = size()
removeRange
protected void removeRange(int start, int end)Parameters
startint- the index at which to start removing.
endint- the index after the last element to remove.
Throws
UnsupportedOperationException- if removing from this list is not supported.
IndexOutOfBoundsException- if
start = size().
set
public E set(int location, E object)Parameters
locationint- the index at which to put the specified object.
objectE- the object to add.
Returns
Throws
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 = size()
subList
public List<E> subList(int start, int end)Returns a part of consecutive elements of this list as a view. The returned view will be of zero length if start equals end. Any change that occurs in the returned subList will be reflected to the original list, and vice-versa. All the supported optional operations by the original list will also be supported by this subList.
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.
Parameters
startint- start index of the subList (inclusive).
endint- end index of the subList, (exclusive).
Returns
start
(inclusive), and ending with end (exclusive)Throws
IndexOutOfBoundsException- if (start size())
IllegalArgumentException- if (start > end)
toArray
public Object[] toArray()ArrayList.Returns
ArrayListtoArray
public <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.Parameters
contentsT[]- the array.
Returns
ArrayList.Throws
ArrayStoreException- when the type of an element in this
ArrayListcannot be stored in the type of the specified array.