public abstract class AbstractList extends AbstractCollection implements List
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 |
---|---|
void |
add(int location,
Object object)
Inserts the specified object into this List at the specified location.
|
boolean |
add(Object object)
Adds the specified object at the end of this List.
|
boolean |
addAll(int location,
Collection 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 Object |
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 |
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 |
listIterator()
Returns a ListIterator on the elements of this list.
|
ListIterator |
listIterator(int location)
Returns a list iterator on the elements of this list.
|
Object |
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.
|
Object |
set(int location,
Object object)
Replaces the element at the specified location in this list with the
specified object.
|
List |
subList(int start,
int end)
Returns a part of consecutive elements of this list as a view.
|
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, size, toArray, toArray, toString
protected AbstractList()
public void add(int location, Object object)
Concrete implementations that would like to support the add functionality must override this method.
add
in interface List
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(Object object)
add
in interface Collection
add
in interface List
add
in class AbstractCollection
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 collection)
addAll
in interface List
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
clear
in interface List
clear
in class AbstractCollection
UnsupportedOperationException
- if removing from this list is not supported.List.isEmpty()
,
List.size()
public boolean equals(Object object)
equals
in interface Collection
equals
in interface List
equals
in class Object
object
- the object to compare to this object.true
if the specified object is equal to this list,
false
otherwise.hashCode()
public abstract Object get(int location)
get
in interface List
location
- the index of the element to return.IndexOutOfBoundsException
- if location < 0 || >= size()
public int hashCode()
hashCode
in interface Collection
hashCode
in interface List
hashCode
in class Object
equals(java.lang.Object)
,
List.hashCode()
public int indexOf(Object object)
public Iterator iterator()
iterator
in interface Collection
iterator
in interface List
iterator
in class AbstractCollection
Iterator
public int lastIndexOf(Object object)
lastIndexOf
in interface List
object
- the object to search for.public ListIterator listIterator()
listIterator
in interface List
ListIterator
public ListIterator listIterator(int location)
listIterator
in interface List
location
- the index at which to start the iteration.IndexOutOfBoundsException
- if location < 0 || location > size()
ListIterator
public Object remove(int location)
remove
in interface List
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 Object set(int location, Object object)
set
in interface List
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 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
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)