Interface List<E>
- All Superinterfaces:
Collection<E>, Iterable<E>
- All Known Implementing Classes:
AbstractList, AbstractSequentialList, ArrayList, LinkedList, Stack, Vector
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, Lists allow duplicate
elements, as compared to Sets, where elements have to be unique.-
Method Summary
Modifier and TypeMethodDescriptionvoidInserts the specified object into thisListat the specified location.booleanAdds the specified object at the end of thisList.booleanaddAll(int location, Collection<? extends E> collection) Inserts the objects in the specified collection at the specified location in thisList.booleanaddAll(Collection<? extends E> collection) Adds the objects in the specified collection to the end of thisList.voidclear()Removes all elements from thisList, leaving it empty.booleanTests whether thisListcontains the specified object.booleancontainsAll(Collection<?> collection) Tests whether thisListcontains all objects contained in the specified collection.booleanCompares the given object with theList, and returns true if they represent the same object using a class specific comparison.get(int location) Returns the element at the specified location in thisList.inthashCode()Returns the hash code for thisList.intSearches thisListfor the specified object and returns the index of the first occurrence.booleanisEmpty()Returns whether thisListcontains no elements.iterator()Returns an iterator on the elements of thisList.intlastIndexOf(Object object) Searches thisListfor the specified object and returns the index of the last occurrence.Returns aListiterator on the elements of thisList.listIterator(int location) Returns a list iterator on the elements of thisList.remove(int location) Removes the object at the specified location from thisList.booleanRemoves the first occurrence of the specified object from thisList.booleanremoveAll(Collection<?> collection) Removes all occurrences in thisListof each object in the specified collection.booleanretainAll(Collection<?> collection) Removes all objects from thisListthat are not contained in the specified collection.Replaces the element at the specified location in thisListwith the specified object.intsize()Returns the number of elements in thisList.subList(int start, int end) Returns aListof the specified portion of thisListfrom 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 Details
-
add
Inserts the specified object into this
Listat 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
-
UnsupportedOperationException: if adding to thisListis not supported. -
ClassCastException: @throws ClassCastException if the class of the object is inappropriate for thisList. -
IllegalArgumentException: if the object cannot be added to thisList. -
IndexOutOfBoundsException: iflocation size()
-
-
add
Adds the specified object at the end of this
List.Parameters
object: the object to add.
Returns
always true.
Throws
-
UnsupportedOperationException: if adding to thisListis not supported. -
ClassCastException: @throws ClassCastException if the class of the object is inappropriate for thisList. -
IllegalArgumentException: if the object cannot be added to thisList.
- Specified by:
addin interfaceCollection<E>- Returns:
trueif thisCollectionis modified,falseotherwise.Throws
-
UnsupportedOperationException: if adding to thisCollectionis not supported. -
ClassCastException: @throws ClassCastException if the class of the object is inappropriate for this collection. -
IllegalArgumentException: if the object cannot be added to thisCollection. -
NullPointerException: if null elements cannot be added to theCollection.
-
-
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.Parameters
-
location: the index at which to insert. -
collection: the collection of objects to be inserted.
Returns
- Returns:
true if this
Listhas been modified through the insertion, false otherwise (i.e. if the passed collection was empty).Throws
-
UnsupportedOperationException: if adding to thisListis not supported. -
ClassCastException: @throws ClassCastException if the class of an object is inappropriate for thisList. -
IllegalArgumentException: if an object cannot be added to thisList. -
IndexOutOfBoundsException: iflocation size()
-
-
-
addAll
Adds the objects in the specified collection to the end of this
List. The objects are added in the order in which they are returned from the collection's iterator.Parameters
collection: the collection of objects.
Returns
- Specified by:
addAllin interfaceCollection<E>- Returns:
trueif thisListis modified,falseotherwise (i.e. if the passed collection was empty).Throws
-
UnsupportedOperationException: if adding to thisListis not supported. -
ClassCastException: @throws 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 this
List, leaving it empty.Throws
UnsupportedOperationException: if removing from thisListis not supported.
See also
-
#isEmpty
-
#size
- Specified by:
clearin interfaceCollection<E>
-
contains
Tests whether this
Listcontains the specified object.Parameters
object: the object to search for.
Returns
- Specified by:
containsin interfaceCollection<E>- Returns:
trueif object is an element of thisList,falseotherwise
-
containsAll
Tests whether this
Listcontains all objects contained in the specified collection.Parameters
collection: the collection of objects
Returns
- Specified by:
containsAllin interfaceCollection<E>- Returns:
trueif all objects in the specified collection are elements of thisList,falseotherwise.
-
equals
Compares the given object with the
List, and returns true if they represent the same object using a class specific comparison. ForLists, this means that they contain the same elements in exactly the same order.Parameters
object: the object to compare with this object.
Returns
- Specified by:
equalsin interfaceCollection<E>- Overrides:
equalsin classObject- Returns:
boolean
trueif the object is the same as this object, andfalseif it is different from this object.See also
- #hashCode
-
get
Returns the element at the specified location in this
List.Parameters
location: the index of the element to return.
Returns
the element at the specified location.
Throws
IndexOutOfBoundsException: iflocation = size()
-
hashCode
int hashCode()Returns the hash code for this
List. It is calculated by taking each element' hashcode and its position in theListinto account.Returns
the hash code of the
List.- Specified by:
hashCodein interfaceCollection<E>- Overrides:
hashCodein classObject
-
indexOf
Searches this
Listfor the specified object and returns the index of the first occurrence.Parameters
object: the object to search for.
Returns
- Returns:
- the index of the first occurrence of the object or -1 if the object was not found.
-
isEmpty
boolean isEmpty()Returns whether this
Listcontains no elements.Returns
- Specified by:
isEmptyin interfaceCollection<E>- Returns:
trueif thisListhas no elements,falseotherwise.See also
- #size
-
iterator
Returns an iterator on the elements of this
List. The elements are iterated in the same order as they occur in theList.Returns
an iterator on the elements of this
List.See also
- Iterator
-
lastIndexOf
Searches this
Listfor the specified object and returns the index of the last occurrence.Parameters
object: the object to search for.
Returns
- Returns:
- the index of the last occurrence of the object, or -1 if the object was not found.
-
listIterator
ListIterator<E> listIterator()Returns a
Listiterator on the elements of thisList. The elements are iterated in the same order that they occur in theList.Returns
a
Listiterator on the elements of thisListSee 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 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 size()
See also
- ListIterator
-
remove
Removes the object at the specified location from this
List.Parameters
location: the index of the object to remove.
Returns
the removed object.
Throws
-
UnsupportedOperationException: if removing from thisListis not supported. -
IndexOutOfBoundsException: iflocation = size()
-
remove
Removes the first occurrence of the specified object from this
List.Parameters
object: the object to remove.
Returns
- Specified by:
removein interfaceCollection<E>- Returns:
true if this
Listwas modified by this operation, false otherwise.Throws
UnsupportedOperationException: if removing from thisListis not supported.
-
removeAll
Removes all occurrences in this
Listof each object in the specified collection.Parameters
collection: the collection of objects to remove.
Returns
trueif thisListis modified,falseotherwise.Throws
UnsupportedOperationException: if removing from thisListis not supported.
- Specified by:
removeAllin interfaceCollection<E>- Returns:
trueif thisCollectionis modified,falseotherwise.Throws
-
UnsupportedOperationException: if removing from thisCollectionis not supported. -
ClassCastException: @throws ClassCastException if one or more elements ofcollectionisn't of the correct type. -
NullPointerException: @throws NullPointerException ifcollectioncontains at least onenullelement and thisCollectiondoesn't supportnullelements. -
NullPointerException: ifcollectionisnull.
-
-
retainAll
Removes all objects from this
Listthat are not contained in the specified collection.Parameters
collection: the collection of objects to retain.
Returns
trueif thisListis modified,falseotherwise.Throws
UnsupportedOperationException: if removing from thisListis not supported.
- Specified by:
retainAllin interfaceCollection<E>- Returns:
trueif thisCollectionis modified,falseotherwise.Throws
-
UnsupportedOperationException: if removing from thisCollectionis not supported. -
ClassCastException: @throws ClassCastException if one or more elements ofcollectionisn't of the correct type. -
NullPointerException: @throws NullPointerException ifcollectioncontains at least onenullelement and thisCollectiondoesn't supportnullelements. -
NullPointerException: ifcollectionisnull.
-
-
set
Replaces the element at the specified location in this
Listwith 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
-
UnsupportedOperationException: if replacing elements in thisListis not supported. -
ClassCastException: @throws ClassCastException if the class of an object is inappropriate for thisList. -
IllegalArgumentException: if an object cannot be added to thisList. -
IndexOutOfBoundsException: iflocation = size()
-
-
size
int size()Returns the number of elements in this
List.Returns
the number of elements in this
List.- Specified by:
sizein interfaceCollection<E>- Returns:
- how many objects this
Collectioncontains, or Integer.MAX_VALUE if there are more than Integer.MAX_VALUE elements in thisCollection.
-
subList
Returns a
Listof the specified portion of thisListfrom the given start index to the end index minus one. The returnedListis backed by thisListso 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: @throws IndexOutOfBoundsException ifstart endorend > size()
-
-
toArray
Object[] toArray()Returns an array containing all elements contained in this
List.Returns
an array of the elements from this
List.- Specified by:
toArrayin interfaceCollection<E>
-
toArray
<T> T[] toArray(T[] array) Returns an array containing all elements contained in this
List. 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.Parameters
array: the array.
Returns
an array of the elements from this
List.Throws
ArrayStoreException: @throws ArrayStoreException if the type of an element in thisListcannot be stored in the type of the specified array.
- Specified by:
toArrayin interfaceCollection<E>
-