Class AbstractList<E>
- All Implemented Interfaces:
Iterable<E>,Collection<E>,List<E>
- Direct Known Subclasses:
AbstractSequentialList,ArrayList,UnsafeArrayList,Vector
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.- Since:
- 1.2
-
Field Summary
Fields Modifier and Type Field Description protected intmodCountA counter for changes to the list. -
Constructor Summary
Constructors Modifier Constructor Description protectedAbstractList()Constructs a new instance of this AbstractList. -
Method Summary
Modifier and Type Method Description voidadd(int location, E object)Inserts the specified object into this List at the specified location.booleanadd(E object)Adds the specified object at the end of this List.booleanaddAll(int location, Collection<? extends E> collection)Inserts the objects in the specified Collection at the specified location in this List.voidclear()Removes all elements from this list, leaving it empty.booleanequals(Object object)Compares the specified object to this list and return true if they are equal.abstract Eget(int location)Returns the element at the specified location in this list.inthashCode()Returns the hash code of this list.intindexOf(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.intlastIndexOf(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.Eremove(int location)Removes the object at the specified location from this list.protected voidremoveRange(int start, int end)Removes the objects in the specified range from the start to the end index minus one.Eset(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.Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, size, toArray, toArray, toString
-
Field Details
-
modCount
protected transient int modCountA counter for changes to the list.
-
-
Constructor Details
-
AbstractList
protected AbstractList()Constructs a new instance of this AbstractList.
-
-
Method Details
-
add
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.
- Specified by:
addin interfaceList<E>- Parameters:
location- the index at which to insert.object- 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 ListIllegalArgumentException- if the object cannot be added to this ListIndexOutOfBoundsException- iflocation < 0 || location > size()
-
add
Adds the specified object at the end of this List.- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceList<E>- Overrides:
addin classAbstractCollection<E>- Parameters:
object- the object to add- Returns:
- true
- Throws:
UnsupportedOperationException- 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 List
-
addAll
Inserts the objects in the specified Collection at the specified location in this List. The objects are added in the order they are returned from the collection's iterator.- Specified by:
addAllin interfaceList<E>- Parameters:
location- the index at which to insert.collection- the Collection of objects- Returns:
trueif this List is modified,falseotherwise.- 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- iflocation < 0 || location > size()
-
clear
public void clear()Removes all elements from this list, leaving it empty.- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceList<E>- Overrides:
clearin classAbstractCollection<E>- Throws:
UnsupportedOperationException- if removing from this list is not supported.- See Also:
List.isEmpty(),List.size()
-
equals
Compares the specified object to this list and return true if they are equal. Two lists are equal when they both contain the same objects in the same order.- Specified by:
equalsin interfaceCollection<E>- Specified by:
equalsin interfaceList<E>- Overrides:
equalsin classObject- Parameters:
object- the object to compare to this object.- Returns:
trueif the specified object is equal to this list,falseotherwise.- See Also:
hashCode()
-
get
Returns the element at the specified location in this list.- Specified by:
getin interfaceList<E>- Parameters:
location- the index of the element to return.- Returns:
- the element at the specified index.
- Throws:
IndexOutOfBoundsException- iflocation < 0 || location >= size()
-
hashCode
public int hashCode()Returns the hash code of this list. The hash code is calculated by taking each element's hashcode into account.- Specified by:
hashCodein interfaceCollection<E>- Specified by:
hashCodein interfaceList<E>- Overrides:
hashCodein classObject- Returns:
- the hash code.
- See Also:
equals(java.lang.Object),List.hashCode()
-
indexOf
Searches this list for the specified object and returns the index of the first occurrence. -
iterator
Returns an iterator on the elements of this list. The elements are iterated in the same order as they occur in the list. -
lastIndexOf
Searches this list for the specified object and returns the index of the last occurrence.- Specified by:
lastIndexOfin interfaceList<E>- Parameters:
object- the object to search for.- Returns:
- the index of the last occurrence of the object, or -1 if the object was not found.
-
listIterator
Returns a ListIterator on the elements of this list. The elements are iterated in the same order that they occur in the list.- Specified by:
listIteratorin interfaceList<E>- Returns:
- a ListIterator on the elements of this list
- See Also:
ListIterator
-
listIterator
Returns a list iterator on the elements of this list. The elements are iterated in the same order as they occur in the list. The iteration starts at the specified location.- Specified by:
listIteratorin interfaceList<E>- Parameters:
location- the index at which to start the iteration.- Returns:
- a ListIterator on the elements of this list.
- Throws:
IndexOutOfBoundsException- iflocation < 0 || location > size()- See Also:
ListIterator
-
remove
Removes the object at the specified location from this list.- Specified by:
removein interfaceList<E>- Parameters:
location- the index of the object to remove.- Returns:
- the removed object.
- Throws:
UnsupportedOperationException- if removing from this list is not supported.IndexOutOfBoundsException- iflocation < 0 || location >= size()
-
removeRange
protected void removeRange(int start, int end)Removes the objects in the specified range from the start to the end index minus one.- Parameters:
start- the index at which to start removing.end- the index after the last element to remove.- Throws:
UnsupportedOperationException- if removing from this list is not supported.IndexOutOfBoundsException- ifstart < 0orstart >= size().
-
set
Replaces the element at the specified location in this list with the specified object.- Specified by:
setin interfaceList<E>- Parameters:
location- the index at which to put the specified object.object- the object to add.- Returns:
- the previous element at the index.
- 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- iflocation < 0 || location >= size()
-
subList
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.
- Specified by:
subListin interfaceList<E>- Parameters:
start- start index of the subList (inclusive).end- end index of the subList, (exclusive).- Returns:
- a subList view of this list starting from
start(inclusive), and ending withend(exclusive) - Throws:
IndexOutOfBoundsException- if (start < 0 || end > size())IllegalArgumentException- if (start > end)
-