Package java.util
Interface List<E>
-
- All Superinterfaces:
Collection<E>
,Iterable<E>
- All Known Implementing Classes:
ArrayList
public interface List<E> extends Collection<E>
AList
is a collection which maintains an ordering for its elements. Every element in theList
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.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
add(int location, E object)
Inserts the specified object into thisList
at the specified location.boolean
add(E object)
Adds the specified object at the end of thisList
.boolean
addAll(int location, Collection<? extends E> collection)
Inserts the objects in the specified collection at the specified location in thisList
.boolean
addAll(Collection<? extends E> collection)
Adds the objects in the specified collection to the end of thisList
.void
clear()
Removes all elements from thisList
, leaving it empty.boolean
contains(Object object)
Tests whether thisList
contains the specified object.boolean
containsAll(Collection<?> collection)
Tests whether thisList
contains all objects contained in the specified collection.boolean
equals(Object object)
Compares the given object with theList
, and returns true if they represent the same object using a class specific comparison.E
get(int location)
Returns the element at the specified location in thisList
.int
hashCode()
Returns the hash code for thisList
.int
indexOf(Object object)
Searches thisList
for the specified object and returns the index of the first occurrence.boolean
isEmpty()
Returns whether thisList
contains no elements.Iterator<E>
iterator()
Returns an iterator on the elements of thisList
.int
lastIndexOf(Object object)
Searches thisList
for the specified object and returns the index of the last occurrence.ListIterator<E>
listIterator()
Returns aList
iterator on the elements of thisList
.ListIterator<E>
listIterator(int location)
Returns a list iterator on the elements of thisList
.E
remove(int location)
Removes the object at the specified location from thisList
.boolean
remove(Object object)
Removes the first occurrence of the specified object from thisList
.boolean
removeAll(Collection<?> collection)
Removes all occurrences in thisList
of each object in the specified collection.boolean
retainAll(Collection<?> collection)
Removes all objects from thisList
that are not contained in the specified collection.E
set(int location, E object)
Replaces the element at the specified location in thisList
with the specified object.int
size()
Returns the number of elements in thisList
.List<E>
subList(int start, int end)
Returns aList
of the specified portion of thisList
from the given start index to the end index minus one.Object[]
toArray()
Returns an array containing all elements contained in thisList
.<T> T[]
toArray(T[] array)
Returns an array containing all elements contained in thisList
.
-
-
-
Method Detail
-
add
void add(int location, E object)
Inserts the specified object into thisList
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 thisList
, the object is added at the end. If the location is smaller than the size of thisList
, then all elements beyond the specified location are moved by one position towards the end of theList
.- Parameters:
location
- the index at which to insert.object
- the object to add.- Throws:
java.lang.UnsupportedOperationException
- if adding to thisList
is not supported.ClassCastException
- if the class of the object is inappropriate for thisList
.IllegalArgumentException
- if the object cannot be added to thisList
.IndexOutOfBoundsException
- iflocation < 0 || location > size()
-
add
boolean add(E object)
Adds the specified object at the end of thisList
.- Specified by:
add
in interfaceCollection<E>
- Parameters:
object
- the object to add.- Returns:
- always true.
- Throws:
java.lang.UnsupportedOperationException
- if adding to thisList
is not supported.ClassCastException
- if the class of the object is inappropriate for thisList
.IllegalArgumentException
- if the object cannot be added to thisList
.
-
addAll
boolean addAll(int location, Collection<? extends E> collection)
Inserts the objects in the specified collection at the specified location in thisList
. The objects are added in the order they are returned from the collection's iterator.- Parameters:
location
- the index at which to insert.collection
- the collection of objects to be inserted.- Returns:
- true if this
List
has been modified through the insertion, false otherwise (i.e. if the passed collection was empty). - Throws:
java.lang.UnsupportedOperationException
- if adding to thisList
is not supported.ClassCastException
- if the class of an object is inappropriate for thisList
.IllegalArgumentException
- if an object cannot be added to thisList
.IndexOutOfBoundsException
- iflocation < 0 || location > size()
-
addAll
boolean addAll(Collection<? extends E> collection)
Adds the objects in the specified collection to the end of thisList
. The objects are added in the order in which they are returned from the collection's iterator.- Specified by:
addAll
in interfaceCollection<E>
- Parameters:
collection
- the collection of objects.- Returns:
true
if thisList
is modified,false
otherwise (i.e. if the passed collection was empty).- Throws:
java.lang.UnsupportedOperationException
- if adding to thisList
is not supported.ClassCastException
- if the class of an object is inappropriate for thisList
.IllegalArgumentException
- if an object cannot be added to thisList
.
-
clear
void clear()
Removes all elements from thisList
, leaving it empty.- Specified by:
clear
in interfaceCollection<E>
- Throws:
java.lang.UnsupportedOperationException
- if removing from thisList
is not supported.- See Also:
isEmpty()
,size()
-
contains
boolean contains(Object object)
Tests whether thisList
contains the specified object.- Specified by:
contains
in interfaceCollection<E>
- Parameters:
object
- the object to search for.- Returns:
true
if object is an element of thisList
,false
otherwise
-
containsAll
boolean containsAll(Collection<?> collection)
Tests whether thisList
contains all objects contained in the specified collection.- Specified by:
containsAll
in interfaceCollection<E>
- Parameters:
collection
- the collection of objects- Returns:
true
if all objects in the specified collection are elements of thisList
,false
otherwise.
-
equals
boolean equals(Object object)
Compares the given object with theList
, and returns true if they represent the same object using a class specific comparison. ForList
s, this means that they contain the same elements in exactly the same order.- Specified by:
equals
in interfaceCollection<E>
- Overrides:
equals
in classObject
- Parameters:
object
- the object to compare with this object.- Returns:
- boolean
true
if the object is the same as this object, andfalse
if it is different from this object. - See Also:
hashCode()
-
get
E get(int location)
Returns the element at the specified location in thisList
.- Parameters:
location
- the index of the element to return.- Returns:
- the element at the specified location.
- Throws:
IndexOutOfBoundsException
- iflocation < 0 || location >= size()
-
hashCode
int hashCode()
Returns the hash code for thisList
. It is calculated by taking each element' hashcode and its position in theList
into account.- Specified by:
hashCode
in interfaceCollection<E>
- Overrides:
hashCode
in classObject
- Returns:
- the hash code of the
List
. - See Also:
Object.equals(java.lang.Object)
-
indexOf
int indexOf(Object object)
Searches thisList
for the specified object and returns the index of the first occurrence.- Parameters:
object
- the object to search for.- Returns:
- the index of the first occurrence of the object or -1 if the object was not found.
-
isEmpty
boolean isEmpty()
Returns whether thisList
contains no elements.- Specified by:
isEmpty
in interfaceCollection<E>
- Returns:
true
if thisList
has no elements,false
otherwise.- See Also:
size()
-
iterator
Iterator<E> iterator()
Returns an iterator on the elements of thisList
. The elements are iterated in the same order as they occur in theList
.
-
lastIndexOf
int lastIndexOf(Object object)
Searches thisList
for the specified object and returns the index of the last occurrence.- 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
ListIterator<E> listIterator()
Returns aList
iterator on the elements of thisList
. The elements are iterated in the same order that they occur in theList
.- Returns:
- a
List
iterator on the elements of thisList
- See Also:
ListIterator
-
listIterator
ListIterator<E> listIterator(int location)
Returns a list iterator on the elements of thisList
. The elements are iterated in the same order as they occur in theList
. The iteration starts at the specified location.- Parameters:
location
- the index at which to start the iteration.- Returns:
- a list iterator on the elements of this
List
. - Throws:
IndexOutOfBoundsException
- iflocation < 0 || location > size()
- See Also:
ListIterator
-
remove
E remove(int location)
Removes the object at the specified location from thisList
.- Parameters:
location
- the index of the object to remove.- Returns:
- the removed object.
- Throws:
java.lang.UnsupportedOperationException
- if removing from thisList
is not supported.IndexOutOfBoundsException
- iflocation < 0 || location >= size()
-
remove
boolean remove(Object object)
Removes the first occurrence of the specified object from thisList
.- Specified by:
remove
in interfaceCollection<E>
- Parameters:
object
- the object to remove.- Returns:
- true if this
List
was modified by this operation, false otherwise. - Throws:
java.lang.UnsupportedOperationException
- if removing from thisList
is not supported.
-
removeAll
boolean removeAll(Collection<?> collection)
Removes all occurrences in thisList
of each object in the specified collection.- Specified by:
removeAll
in interfaceCollection<E>
- Parameters:
collection
- the collection of objects to remove.- Returns:
true
if thisList
is modified,false
otherwise.- Throws:
java.lang.UnsupportedOperationException
- if removing from thisList
is not supported.
-
retainAll
boolean retainAll(Collection<?> collection)
Removes all objects from thisList
that are not contained in the specified collection.- Specified by:
retainAll
in interfaceCollection<E>
- Parameters:
collection
- the collection of objects to retain.- Returns:
true
if thisList
is modified,false
otherwise.- Throws:
java.lang.UnsupportedOperationException
- if removing from thisList
is not supported.
-
set
E set(int location, E object)
Replaces the element at the specified location in thisList
with the specified object. This operation does not change the size of theList
.- Parameters:
location
- the index at which to put the specified object.object
- the object to insert.- Returns:
- the previous element at the index.
- Throws:
java.lang.UnsupportedOperationException
- if replacing elements in thisList
is not supported.ClassCastException
- if the class of an object is inappropriate for thisList
.IllegalArgumentException
- if an object cannot be added to thisList
.IndexOutOfBoundsException
- iflocation < 0 || location >= size()
-
size
int size()
Returns the number of elements in thisList
.- Specified by:
size
in interfaceCollection<E>
- Returns:
- the number of elements in this
List
.
-
subList
List<E> subList(int start, int end)
Returns aList
of the specified portion of thisList
from the given start index to the end index minus one. The returnedList
is backed by thisList
so changes to it are reflected by the other.- Parameters:
start
- the index at which to start the sublist.end
- the index one past the end of the sublist.- Returns:
- a list of a portion of this
List
. - Throws:
IndexOutOfBoundsException
- ifstart < 0, start > end
orend > size()
-
toArray
Object[] toArray()
Returns an array containing all elements contained in thisList
.- Specified by:
toArray
in interfaceCollection<E>
- Returns:
- an array of the elements from this
List
.
-
toArray
<T> T[] toArray(T[] array)
Returns an array containing all elements contained in thisList
. 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 thisList
, the array element following the collection elements is set to null.- Specified by:
toArray
in interfaceCollection<E>
- Type Parameters:
T
- the type.- Parameters:
array
- the array.- Returns:
- an array of the elements from this
List
. - Throws:
ArrayStoreException
- if the type of an element in thisList
cannot be stored in the type of the specified array.
-
-