Skip to content

Most visited

Recently visited

navigation
Added in API level 1

List

public interface List
implements Collection<E>

java.util.List<E>
Known Indirect Subclasses


A 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.

Summary

Public methods

abstract boolean add(E object)

Adds the specified object at the end of this List.

abstract void add(int location, E object)

Inserts the specified object into this List at the specified location.

abstract boolean addAll(Collection<? extends E> collection)

Adds the objects in the specified collection to the end of this List.

abstract boolean addAll(int location, Collection<? extends E> collection)

Inserts the objects in the specified collection at the specified location in this List.

abstract void clear()

Removes all elements from this List, leaving it empty.

abstract boolean contains(Object object)

Tests whether this List contains the specified object.

abstract boolean containsAll(Collection<?> collection)

Tests whether this List contains all objects contained in the specified collection.

abstract boolean equals(Object object)

Compares the given object with the List, and returns true if they represent the same object using a class specific comparison.

abstract E get(int location)

Returns the element at the specified location in this List.

abstract int hashCode()

Returns the hash code for this List.

abstract int indexOf(Object object)

Searches this List for the specified object and returns the index of the first occurrence.

abstract boolean isEmpty()

Returns whether this List contains no elements.

abstract Iterator<E> iterator()

Returns an iterator on the elements of this List.

abstract int lastIndexOf(Object object)

Searches this List for the specified object and returns the index of the last occurrence.

abstract ListIterator<E> listIterator(int location)

Returns a list iterator on the elements of this List.

abstract ListIterator<E> listIterator()

Returns a List iterator on the elements of this List.

abstract E remove(int location)

Removes the object at the specified location from this List.

abstract boolean remove(Object object)

Removes the first occurrence of the specified object from this List.

abstract boolean removeAll(Collection<?> collection)

Removes all occurrences in this List of each object in the specified collection.

abstract boolean retainAll(Collection<?> collection)

Removes all objects from this List that are not contained in the specified collection.

abstract E set(int location, E object)

Replaces the element at the specified location in this List with the specified object.

abstract int size()

Returns the number of elements in this List.

abstract List<E> subList(int start, int end)

Returns a List of the specified portion of this List from the given start index to the end index minus one.

abstract <T> T[] toArray(T[] array)

Returns an array containing all elements contained in this List.

abstract Object[] toArray()

Returns an array containing all elements contained in this List.

Inherited methods

From interface java.util.Collection
From interface java.lang.Iterable

Public methods

add

Added in API level 1
boolean add (E object)

Adds the specified object at the end of this List.

Parameters
object E: the object to add.
Returns
boolean always true.
Throws
UnsupportedOperationException if adding to this List is not supported.
ClassCastException if the class of the object is inappropriate for this List.
IllegalArgumentException if the object cannot be added to this List.

add

Added in API level 1
void add (int location, 
                E object)

Inserts the specified object into this 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.

Parameters
location int: the index at which to insert.
object E: 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 List.
IllegalArgumentException if the object cannot be added to this List.
IndexOutOfBoundsException if location < 0 || location > size()

addAll

Added in API level 1
boolean addAll (Collection<? extends E> collection)

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 Collection: the collection of objects.
Returns
boolean true if this List is modified, false otherwise (i.e. if the passed collection was empty).
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.
NullPointerException if collection is null.

addAll

Added in API level 1
boolean addAll (int location, 
                Collection<? extends E> collection)

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 int: the index at which to insert.
collection Collection: the collection of objects to be inserted.
Returns
boolean true if this List has been modified through the insertion, false otherwise (i.e. if the passed collection was empty).
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 if location < 0 || location > size().
NullPointerException if collection is null.

clear

Added in API level 1
void clear ()

Removes all elements from this List, leaving it empty.

Throws
UnsupportedOperationException if removing from this List is not supported.

See also:

contains

Added in API level 1
boolean contains (Object object)

Tests whether this List contains the specified object.

Parameters
object Object: the object to search for.
Returns
boolean true if object is an element of this List, false otherwise

containsAll

Added in API level 1
boolean containsAll (Collection<?> collection)

Tests whether this List contains all objects contained in the specified collection.

Parameters
collection Collection: the collection of objects
Returns
boolean true if all objects in the specified collection are elements of this List, false otherwise.
Throws
NullPointerException if collection is null.

equals

Added in API level 1
boolean equals (Object object)

Compares the given object with the List, and returns true if they represent the same object using a class specific comparison. For Lists, this means that they contain the same elements in exactly the same order.

Parameters
object Object: the object to compare with this object.
Returns
boolean boolean true if the object is the same as this object, and false if it is different from this object.

See also:

get

Added in API level 1
E get (int location)

Returns the element at the specified location in this List.

Parameters
location int: the index of the element to return.
Returns
E the element at the specified location.
Throws
IndexOutOfBoundsException if location < 0 || location >= size()

hashCode

Added in API level 1
int hashCode ()

Returns the hash code for this List. It is calculated by taking each element' hashcode and its position in the List into account.

Returns
int the hash code of the List.

indexOf

Added in API level 1
int indexOf (Object object)

Searches this List for the specified object and returns the index of the first occurrence.

Parameters
object Object: the object to search for.
Returns
int the index of the first occurrence of the object or -1 if the object was not found.

isEmpty

Added in API level 1
boolean isEmpty ()

Returns whether this List contains no elements.

Returns
boolean true if this List has no elements, false otherwise.

See also:

iterator

Added in API level 1
Iterator<E> iterator ()

Returns an iterator on the elements of this List. The elements are iterated in the same order as they occur in the List.

Returns
Iterator<E> an iterator on the elements of this List.

See also:

lastIndexOf

Added in API level 1
int lastIndexOf (Object object)

Searches this List for the specified object and returns the index of the last occurrence.

Parameters
object Object: the object to search for.
Returns
int the index of the last occurrence of the object, or -1 if the object was not found.

listIterator

Added in API level 1
ListIterator<E> listIterator (int location)

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.

Parameters
location int: the index at which to start the iteration.
Returns
ListIterator<E> a list iterator on the elements of this List.
Throws
IndexOutOfBoundsException if location < 0 || location > size()

See also:

listIterator

Added in API level 1
ListIterator<E> listIterator ()

Returns a List iterator on the elements of this List. The elements are iterated in the same order that they occur in the List.

Returns
ListIterator<E> a List iterator on the elements of this List

See also:

remove

Added in API level 1
E remove (int location)

Removes the object at the specified location from this List.

Parameters
location int: the index of the object to remove.
Returns
E the removed object.
Throws
UnsupportedOperationException if removing from this List is not supported.
IndexOutOfBoundsException if location < 0 || location >= size()

remove

Added in API level 1
boolean remove (Object object)

Removes the first occurrence of the specified object from this List.

Parameters
object Object: the object to remove.
Returns
boolean true if this List was modified by this operation, false otherwise.
Throws
UnsupportedOperationException if removing from this List is not supported.

removeAll

Added in API level 1
boolean removeAll (Collection<?> collection)

Removes all occurrences in this List of each object in the specified collection.

Parameters
collection Collection: the collection of objects to remove.
Returns
boolean true if this List is modified, false otherwise.
Throws
UnsupportedOperationException if removing from this List is not supported.
NullPointerException if collection is null.

retainAll

Added in API level 1
boolean retainAll (Collection<?> collection)

Removes all objects from this List that are not contained in the specified collection.

Parameters
collection Collection: the collection of objects to retain.
Returns
boolean true if this List is modified, false otherwise.
Throws
UnsupportedOperationException if removing from this List is not supported.
NullPointerException if collection is null.

set

Added in API level 1
E set (int location, 
                E object)

Replaces the element at the specified location in this List with the specified object. This operation does not change the size of the List.

Parameters
location int: the index at which to put the specified object.
object E: the object to insert.
Returns
E 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 if location < 0 || location >= size()

size

Added in API level 1
int size ()

Returns the number of elements in this List.

Returns
int the number of elements in this List.

subList

Added in API level 1
List<E> subList (int start, 
                int end)

Returns a List of the specified portion of this List from the given start index to the end index minus one. The returned List is backed by this List so changes to it are reflected by the other.

Parameters
start int: the index at which to start the sublist.
end int: the index one past the end of the sublist.
Returns
List<E> a list of a portion of this List.
Throws
IndexOutOfBoundsException if start < 0, start > end or end > size()

toArray

Added in API level 1
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 this List, the array element following the collection elements is set to null.

Parameters
array T: the array.
Returns
T[] an array of the elements from this List.
Throws
ArrayStoreException if the type of an element in this List cannot be stored in the type of the specified array.

toArray

Added in API level 1
Object[] toArray ()

Returns an array containing all elements contained in this List.

Returns
Object[] an array of the elements from this List.
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.