public abstract class List<E> extends Collection<E>
List
is a collection which maintains an ordering for its elements. Every element in the List
has an
index. Each element can thus be accessed by its index, with the first index being zero. Normally, List
s allow
duplicate elements, as compared to Sets, where elements have to be unique.
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
.
Changes from the java.lang version: Removed subList method. It's rarely used (in my experience) and eliminated a
fair amount of code/complexity.Modifier | Constructor and Description |
---|---|
protected |
List()
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.
|
boolean |
equals(java.lang.Object object)
By default, equals isn't supported for lists or other collections, though subclasses can choose to override this
behavior if they want.
|
abstract E |
get(int location)
Returns the element at the specified location in this list.
|
int |
hashCode()
Returns the hash code of this list.
|
abstract int |
indexOf(E object)
Searches this
List for the specified object and returns the index of the first occurrence. |
abstract int |
lastIndexOf(E object)
Searches this
List for the specified object and returns the index of the last occurrence. |
abstract E |
remove(int location)
Removes the object at the specified location from this
List . |
abstract E |
set(int location,
E object)
Replaces the element at the specified location in this
List with the specified object. |
addAll, clear, contains, containsAll, isEmpty, iterator, remove, removeAll, retainAll, size, toArray, toArray, toString
public void add(int location, E object)
List
at the specified location. The object is inserted before the
current element at the specified location. If the location is equal to the size of this List
, the object
is added at the end. If the location is smaller than the size of this List
, then all elements beyond the
specified location are moved by one position towards the end of the List
.
Concrete implementations that would like to support the add functionality must override this method.location
- the index at which to insert.object
- the object to add.java.lang.UnsupportedOperationException
- if adding to this List is not supported.java.lang.ClassCastException
- if the class of the object is inappropriate for this Listjava.lang.IllegalArgumentException
- if the object cannot be added to this Listjava.lang.IndexOutOfBoundsException
- if location < 0 || >= size()
public boolean add(E object)
add
in class Collection<E>
object
- the object to addjava.lang.UnsupportedOperationException
- if adding to this List is not supportedjava.lang.ClassCastException
- if the class of the object is inappropriate for this Listjava.lang.IllegalArgumentException
- if the object cannot be added to this Listpublic boolean addAll(int location, Collection<? extends E> collection)
location
- the index at which to insert.collection
- the Collection of objectstrue
if this List is modified, false
otherwise.java.lang.UnsupportedOperationException
- if adding to this list is not supported.java.lang.ClassCastException
- if the class of an object is inappropriate for this list.java.lang.IllegalArgumentException
- if an object cannot be added to this list.java.lang.IndexOutOfBoundsException
- if location < 0 || > size()
public boolean equals(java.lang.Object object)
equals
in class java.lang.Object
object
- the object to compare to this object.true
if the specified object is equal to this list, false
otherwise; though by default
this method throws an exception and developers generally shouldn't use itpublic abstract E get(int location)
location
- the index of the element to return.java.lang.IndexOutOfBoundsException
- if location < 0 || >= size()
public int hashCode()
hashCode
in class java.lang.Object
equals(java.lang.Object)
,
hashCode()
public abstract int indexOf(E object)
List
for the specified object and returns the index of the first occurrence.object
- the object to search for.public abstract int lastIndexOf(E object)
List
for the specified object and returns the index of the last occurrence.object
- the object to search for.public abstract E remove(int location)
List
.location
- the index of the object to remove.java.lang.UnsupportedOperationException
- if removing from this List
is not supported.java.lang.IndexOutOfBoundsException
- if location < 0 || >= size()
public abstract E set(int location, E object)
List
with the specified object. This operation
does not change the size of the List
.location
- the index at which to put the specified object.object
- the object to insert.java.lang.UnsupportedOperationException
- if replacing elements in this List
is not supported.java.lang.ClassCastException
- if the class of an object is inappropriate for this List
.java.lang.IllegalArgumentException
- if an object cannot be added to this List
.java.lang.IndexOutOfBoundsException
- if location < 0 || >= size()