Download as pdf or txt
Download as pdf or txt
You are on page 1of 18

11/24/2017 ArrayList | Android Developers

added in API level 1

ArrayList
(https://developer.android.com/guide/topics/manifest/uses-sdk-
element.html#ApiLevels)
Summary: Inherited Fields (#inh elds) | Ctors (#pubctors) |
public class ArrayList Methods (#pubmethods) | Protected Methods (#promethods) |
Inherited Methods (#inhmethods) | [Expand All] (#)
extends AbstractList (https://developer.android.com/reference/java/util/AbstractList.html)<E>

implements List (https://developer.android.com/reference/java/util/List.html)<E>, RandomAccess


(https://developer.android.com/reference/java/util/RandomAccess.html), Cloneable (https://developer.android.com/reference/java/lang/Cloneable.html),

Serializable (https://developer.android.com/reference/java/io/Serializable.html)

java.lang.Object (https://developer.android.com/reference/java/lang/Object.html)
   ↳ java.util.AbstractCollection (https://developer.android.com/reference/java/util/AbstractCollection.html)<E>
     ↳ java.util.AbstractList (https://developer.android.com/reference/java/util/AbstractList.html)<E>
       ↳ java.util.ArrayList<E>

(#)Known Direct Subclasses

ObservableArrayList (https://developer.android.com/reference/android/databinding/ObservableArrayList.html)<T>

Resizable-array implementation of the List interface. Implements all optional list operations, and permits all elements, including null. In addition to
implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. (This class is
roughly equivalent to Vector, except that it is unsynchronized.)

The size, isEmpty, get, set, iterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is,
adding n elements requires O(n) time. All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for
the LinkedList implementation.

Each ArrayList instance has a capacity. The capacity is the size of the array used to store the elements in the list. It is always at least as large as the
list size. As elements are added to an ArrayList, its capacity grows automatically. The details of the growth policy are not speci ed beyond the fact that
adding an element has constant amortized time cost.

An application can increase the capacity of an ArrayList instance before adding a large number of elements using the ensureCapacity operation.
This may reduce the amount of incremental reallocation.

Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads
modi es the list structurally, it must be synchronized externally. (A structural modi cation is any operation that adds or deletes one or more elements, or
explicitly resizes the backing array; merely setting the value of an element is not a structural modi cation.) This is typically accomplished by
synchronizing on some object that naturally encapsulates the list. If no such object exists, the list should be "wrapped" using the
Collections.synchronizedList (https://developer.android.com/reference/java/util/Collections.html#synchronizedList(java.util.List<T>)) method. This is best
done at creation time, to prevent accidental unsynchronized access to the list:

List list = Collections.synchronizedList(new ArrayList(...));

The iterators returned by this class's iterator (https://developer.android.com/reference/java/util/ArrayList.html#iterator()) and listIterator


(https://developer.android.com/reference/java/util/ArrayList.html#listIterator(int)) methods are fail-fast: if the list is structurally modi ed at any time after
the iterator is created, in any way except through the iterator's own remove (https://developer.android.com/reference/java/util/ListIterator.html#remove()) or
add (https://developer.android.com/reference/java/util/ListIterator.html#add(E)) methods, the iterator will throw a ConcurrentModificationException
(https://developer.android.com/reference/java/util/ConcurrentModificationException.html). Thus, in the face of concurrent modi cation, the iterator fails quickly
and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.

https://developer.android.com/reference/java/util/ArrayList.html 1/18
11/24/2017 ArrayList | Android Developers
Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence
of unsynchronized concurrent modi cation. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be
wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.

This class is a member of the Java Collections Framework (https://developer.android.com/openjdk-redirect.html?v=8&path=/technotes/guides/collections/index.html).

See also:

Collection (https://developer.android.com/reference/java/util/Collection.html)

List (https://developer.android.com/reference/java/util/List.html)

LinkedList (https://developer.android.com/reference/java/util/LinkedList.html)

Vector (https://developer.android.com/reference/java/util/Vector.html)

Summary

Inherited elds

(#)From class java.util.AbstractList (https://developer.android.com/reference/java/util/AbstractList.html)

Public constructors

ArrayList (https://developer.android.com/reference/java/util/ArrayList.html#ArrayList(int))(int initialCapacity)


Constructs an empty list with the speci ed initial capacity.

ArrayList (https://developer.android.com/reference/java/util/ArrayList.html#ArrayList())()

Constructs an empty list with an initial capacity of ten.

ArrayList (https://developer.android.com/reference/java/util/ArrayList.html#ArrayList(java.util.Collection<? extends E>))(Collection

(https://developer.android.com/reference/java/util/Collection.html)<? extends E> c)


Constructs a list containing the elements of the speci ed collection, in the order they are returned by the collection's iterator.

Public methods

boolean add (https://developer.android.com/reference/java/util/ArrayList.html#add(E))(E e)


Appends the speci ed element to the end of this list.

void add (https://developer.android.com/reference/java/util/ArrayList.html#add(int, E))(int index, E element)


Inserts the speci ed element at the speci ed position in this list.

boolean addAll (https://developer.android.com/reference/java/util/ArrayList.html#addAll(java.util.Collection<? extends E>))(Collection

(https://developer.android.com/reference/java/util/Collection.html)<? extends E> c)


Appends all of the elements in the speci ed collection to the end of this list, in the order that they are returned by the
speci ed collection's Iterator.

boolean addAll (https://developer.android.com/reference/java/util/ArrayList.html#addAll(int, java.util.Collection<? extends E>))(int

index, Collection (https://developer.android.com/reference/java/util/Collection.html)<? extends E> c)

https://developer.android.com/reference/java/util/ArrayList.html 2/18
11/24/2017 ArrayList | Android Developers
Inserts all of the elements in the speci ed collection into this list, starting at the speci ed position.

void clear (https://developer.android.com/reference/java/util/ArrayList.html#clear())()

Removes all of the elements from this list.

Object (https://developer.android.com/reference/java/lang/Object.html) clone (https://developer.android.com/reference/java/util/ArrayList.html#clone())()

Returns a shallow copy of this ArrayList instance.

boolean contains (https://developer.android.com/reference/java/util/ArrayList.html#contains(java.lang.Object))(Object

(https://developer.android.com/reference/java/lang/Object.html) o)
Returns true if this list contains the speci ed element.

void ensureCapacity (https://developer.android.com/reference/java/util/ArrayList.html#ensureCapacity(int))(int minCapacity)


Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of
elements speci ed by the minimum capacity argument.

void forEach (https://developer.android.com/reference/java/util/ArrayList.html#forEach(java.util.function.Consumer<? super E>))

(Consumer (https://developer.android.com/reference/java/util/function/Consumer.html)<? super E> action)

E get (https://developer.android.com/reference/java/util/ArrayList.html#get(int))(int index)


Returns the element at the speci ed position in this list.

int indexOf (https://developer.android.com/reference/java/util/ArrayList.html#indexOf(java.lang.Object))(Object

(https://developer.android.com/reference/java/lang/Object.html) o)
Returns the index of the rst occurrence of the speci ed element in this list, or -1 if this list does not contain the element.

boolean isEmpty (https://developer.android.com/reference/java/util/ArrayList.html#isEmpty())()

Returns true if this list contains no elements.

Iterator iterator (https://developer.android.com/reference/java/util/ArrayList.html#iterator())()

(https://developer.android.com/reference/java/util/Iterator.html)<E> Returns an iterator over the elements in this list in proper sequence.

int lastIndexOf (https://developer.android.com/reference/java/util/ArrayList.html#lastIndexOf(java.lang.Object))(Object

(https://developer.android.com/reference/java/lang/Object.html) o)
Returns the index of the last occurrence of the speci ed element in this list, or -1 if this list does not contain the element.

ListIterator listIterator (https://developer.android.com/reference/java/util/ArrayList.html#listIterator(int))(int index)


(https://developer.android.com/reference/java/util/ListIterator.html)<E> Returns a list iterator over the elements in this list (in proper sequence), starting at the speci ed position in the list.

ListIterator listIterator (https://developer.android.com/reference/java/util/ArrayList.html#listIterator())()

(https://developer.android.com/reference/java/util/ListIterator.html)<E> Returns a list iterator over the elements in this list (in proper sequence).

E remove (https://developer.android.com/reference/java/util/ArrayList.html#remove(int))(int index)


Removes the element at the speci ed position in this list.

boolean remove (https://developer.android.com/reference/java/util/ArrayList.html#remove(java.lang.Object))(Object

(https://developer.android.com/reference/java/lang/Object.html) o)
Removes the rst occurrence of the speci ed element from this list, if it is present.

boolean removeAll (https://developer.android.com/reference/java/util/ArrayList.html#removeAll(java.util.Collection<?>))(Collection

(https://developer.android.com/reference/java/util/Collection.html)<?> c)
Removes from this list all of its elements that are contained in the speci ed collection.

boolean removeIf (https://developer.android.com/reference/java/util/ArrayList.html#removeIf(java.util.function.Predicate<? super E>))

(Predicate (https://developer.android.com/reference/java/util/function/Predicate.html)<? super E> filter)


Removes all of the elements of this collection that satisfy the given predicate.

https://developer.android.com/reference/java/util/ArrayList.html 3/18
11/24/2017 ArrayList | Android Developers

void replaceAll (https://developer.android.com/reference/java/util/ArrayList.html#replaceAll(java.util.function.UnaryOperator<E>))

(UnaryOperator (https://developer.android.com/reference/java/util/function/UnaryOperator.html)<E> operator)


Replaces each element of this list with the result of applying the operator to that element.

boolean retainAll (https://developer.android.com/reference/java/util/ArrayList.html#retainAll(java.util.Collection<?>))(Collection

(https://developer.android.com/reference/java/util/Collection.html)<?> c)
Retains only the elements in this list that are contained in the speci ed collection.

E set (https://developer.android.com/reference/java/util/ArrayList.html#set(int, E))(int index, E element)


Replaces the element at the speci ed position in this list with the speci ed element.

int size (https://developer.android.com/reference/java/util/ArrayList.html#size())()

Returns the number of elements in this list.

void sort (https://developer.android.com/reference/java/util/ArrayList.html#sort(java.util.Comparator<? super E>))(Comparator

(https://developer.android.com/reference/java/util/Comparator.html)<? super E> c)


Sorts this list according to the order induced by the speci ed Comparator
(https://developer.android.com/reference/java/util/Comparator.html).

Spliterator spliterator (https://developer.android.com/reference/java/util/ArrayList.html#spliterator())()

(https://developer.android.com/reference/java/util/Spliterator.html)<E> Creates a late-binding (https://developer.android.com/reference/java/util/Spliterator.html#binding) and fail-fast Spliterator


(https://developer.android.com/reference/java/util/Spliterator.html) over the elements in this list.

List (https://developer.android.com/reference/java/util/List.html)<E> subList (https://developer.android.com/reference/java/util/ArrayList.html#subList(int, int))(int fromIndex, int toIndex)


Returns a view of the portion of this list between the speci ed fromIndex, inclusive, and toIndex, exclusive.

Object[] toArray (https://developer.android.com/reference/java/util/ArrayList.html#toArray())()

(https://developer.android.com/reference/java/lang/Object.html) Returns an array containing all of the elements in this list in proper sequence (from rst to last element).

<T> T[] toArray (https://developer.android.com/reference/java/util/ArrayList.html#toArray(T[]))(T[] a)


Returns an array containing all of the elements in this list in proper sequence (from rst to last element); the runtime type
of the returned array is that of the speci ed array.

void trimToSize (https://developer.android.com/reference/java/util/ArrayList.html#trimToSize())()

Trims the capacity of this ArrayList instance to be the list's current size.

Protected methods

void removeRange (https://developer.android.com/reference/java/util/ArrayList.html#removeRange(int, int))(int fromIndex, int toIndex)


Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.

Inherited methods

(#)From class java.util.AbstractList (https://developer.android.com/reference/java/util/AbstractList.html)

(#)From class java.util.AbstractCollection (https://developer.android.com/reference/java/util/AbstractCollection.html)

(#)From class java.lang.Object (https://developer.android.com/reference/java/lang/Object.html)

(#)From interface java.util.List (https://developer.android.com/reference/java/util/List.html)

(#)From interface java.util.Collection (https://developer.android.com/reference/java/util/Collection.html)

https://developer.android.com/reference/java/util/ArrayList.html 4/18
11/24/2017 ArrayList | Android Developers

(#)From interface java.lang.Iterable (https://developer.android.com/reference/java/lang/Iterable.html)

Public constructors

ArrayList added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

ArrayList (int initialCapacity)

Constructs an empty list with the speci ed initial capacity.

Parameters

initialCapacity int: the initial capacity of the list

Throws

IllegalArgumentException if the speci ed initial capacity is negative


(https://developer.android.com/reference/java/lang/IllegalArgumentException.html)

ArrayList added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

ArrayList ()

Constructs an empty list with an initial capacity of ten.

ArrayList added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

ArrayList (Collection (https://developer.android.com/reference/java/util/Collection.html)<? extends E> c)

Constructs a list containing the elements of the speci ed collection, in the order they are returned by the collection's iterator.

Parameters

c Collection: the collection whose elements are to be placed into this list

Throws

NullPointerException if the speci ed collection is null


(https://developer.android.com/reference/java/lang/NullPointerException.html)

https://developer.android.com/reference/java/util/ArrayList.html 5/18
11/24/2017 ArrayList | Android Developers

Public methods

add added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

boolean add (E e)

Appends the speci ed element to the end of this list.

Parameters

e E: element to be appended to this list

Returns

boolean true (as speci ed by add(E) (https://developer.android.com/reference/java/util/Collection.html#add(E)))

add added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

void add (int index,


E element)

Inserts the speci ed element at the speci ed position in this list. Shifts the element currently at that position (if any) and any subsequent elements to
the right (adds one to their indices).

Parameters

index int: index at which the speci ed element is to be inserted

element E: element to be inserted

Throws

IndexOutOfBoundsException
(https://developer.android.com/reference/java/lang/IndexOutOfBoundsException.html)

addAll added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

boolean addAll (Collection (https://developer.android.com/reference/java/util/Collection.html)<? extends E> c)

Appends all of the elements in the speci ed collection to the end of this list, in the order that they are returned by the speci ed collection's Iterator. The
behavior of this operation is unde ned if the speci ed collection is modi ed while the operation is in progress. (This implies that the behavior of this call
is unde ned if the speci ed collection is this list, and this list is nonempty.)

Parameters

https://developer.android.com/reference/java/util/ArrayList.html 6/18
11/24/2017 ArrayList | Android Developers
c Collection: collection containing elements to be added to this list

Returns

boolean true if this list changed as a result of the call

Throws

NullPointerException if the speci ed collection is null


(https://developer.android.com/reference/java/lang/NullPointerException.html)

addAll added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

boolean addAll (int index,


Collection (https://developer.android.com/reference/java/util/Collection.html)<? extends E> c)

Inserts all of the elements in the speci ed collection into this list, starting at the speci ed position. Shifts the element currently at that position (if any)
and any subsequent elements to the right (increases their indices). The new elements will appear in the list in the order that they are returned by the
speci ed collection's iterator.

Parameters

index int: index at which to insert the rst element from the speci ed collection

c Collection: collection containing elements to be added to this list

Returns

boolean true if this list changed as a result of the call

Throws

IndexOutOfBoundsException
(https://developer.android.com/reference/java/lang/IndexOutOfBoundsException.html)

NullPointerException if the speci ed collection is null


(https://developer.android.com/reference/java/lang/NullPointerException.html)

clear added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

void clear ()

Removes all of the elements from this list. The list will be empty after this call returns.

clone
https://developer.android.com/reference/java/util/ArrayList.html 7/18
11/24/2017 ArrayList | Android Developers
Object (https://developer.android.com/reference/java/lang/Object.html) clone () added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

Returns a shallow copy of this ArrayList instance. (The elements themselves are not copied.)

Returns

Object a clone of this ArrayList instance


(https://developer.android.com/reference/java/lang/Object.html)

contains added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

boolean contains (Object (https://developer.android.com/reference/java/lang/Object.html) o)

Returns true if this list contains the speci ed element. More formally, returns true if and only if this list contains at least one element e such that
(o==null ? e==null : o.equals(e)).

Parameters

o Object: element whose presence in this list is to be tested

Returns

boolean true if this list contains the speci ed element

ensureCapacity added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

void ensureCapacity (int minCapacity)

Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements speci ed by the minimum
capacity argument.

Parameters

minCapacity int: the desired minimum capacity

forEach added in API level 24 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

void forEach (Consumer (https://developer.android.com/reference/java/util/function/Consumer.html)<? super E> action)

Parameters

action Consumer

https://developer.android.com/reference/java/util/ArrayList.html 8/18
11/24/2017 ArrayList | Android Developers

get added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

E get (int index)

Returns the element at the speci ed position in this list.

Parameters

index int: index of the element to return

Returns

E the element at the speci ed position in this list

Throws

IndexOutOfBoundsException
(https://developer.android.com/reference/java/lang/IndexOutOfBoundsException.html)

indexOf added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

int indexOf (Object (https://developer.android.com/reference/java/lang/Object.html) o)

Returns the index of the rst occurrence of the speci ed element in this list, or -1 if this list does not contain the element. More formally, returns the
lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.

Parameters

o Object: element to search for

Returns

int the index of the rst occurrence of the speci ed element in this list, or -1 if this list does not contain the element

isEmpty added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

boolean isEmpty ()

Returns true if this list contains no elements.

Returns

boolean true if this list contains no elements

iterator added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

https://developer.android.com/reference/java/util/ArrayList.html 9/18
11/24/2017 ArrayList | Android Developers
Iterator (https://developer.android.com/reference/java/util/Iterator.html)<E> iterator ()

Returns an iterator over the elements in this list in proper sequence.

The returned iterator is fail-fast (#fail-fast).

Returns

Iterator an iterator over the elements in this list in proper sequence


(https://developer.android.com/reference/java/util/Iterator.html)<E>

lastIndexOf added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

int lastIndexOf (Object (https://developer.android.com/reference/java/lang/Object.html) o)

Returns the index of the last occurrence of the speci ed element in this list, or -1 if this list does not contain the element. More formally, returns the
highest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index.

Parameters

o Object: element to search for

Returns

int the index of the last occurrence of the speci ed element in this list, or -1 if this list does not contain the element

listIterator added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

ListIterator (https://developer.android.com/reference/java/util/ListIterator.html)<E> listIterator (int index)

Returns a list iterator over the elements in this list (in proper sequence), starting at the speci ed position in the list. The speci ed index indicates the
rst element that would be returned by an initial call to next (https://developer.android.com/reference/java/util/ListIterator.html#next()). An initial call to
previous (https://developer.android.com/reference/java/util/ListIterator.html#previous()) would return the element with the speci ed index minus one.

The returned list iterator is fail-fast (#fail-fast).

Parameters

index int: index of the rst element to be returned from the list iterator (by a call to next
(https://developer.android.com/reference/java/util/ListIterator.html#next()))

Returns

ListIterator a list iterator over the elements in this list (in proper sequence), starting at
(https://developer.android.com/reference/java/util/ListIterator.html)<E> the speci ed position in the list

Throws

IndexOutOfBoundsException

https://developer.android.com/reference/java/util/ArrayList.html 10/18
11/24/2017 ArrayList | Android Developers
(https://developer.android.com/reference/java/lang/IndexOutOfBoundsException.html)

listIterator added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

ListIterator (https://developer.android.com/reference/java/util/ListIterator.html)<E> listIterator ()

Returns a list iterator over the elements in this list (in proper sequence).

The returned list iterator is fail-fast (#fail-fast).

Returns

ListIterator a list iterator over the elements in this list (in proper sequence)
(https://developer.android.com/reference/java/util/ListIterator.html)<E>

See also:

listIterator(int) (https://developer.android.com/reference/java/util/ArrayList.html#listIterator(int))

remove added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

E remove (int index)

Removes the element at the speci ed position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

Parameters

index int: the index of the element to be removed

Returns

E the element that was removed from the list

Throws

IndexOutOfBoundsException
(https://developer.android.com/reference/java/lang/IndexOutOfBoundsException.html)

remove added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

boolean remove (Object (https://developer.android.com/reference/java/lang/Object.html) o)

Removes the rst occurrence of the speci ed element from this list, if it is present. If the list does not contain the element, it is unchanged. More
formally, removes the element with the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))) (if such an element exists).
Returns true if this list contained the speci ed element (or equivalently, if this list changed as a result of the call).

https://developer.android.com/reference/java/util/ArrayList.html 11/18
11/24/2017 ArrayList | Android Developers

Parameters

o Object: element to be removed from this list, if present

Returns

boolean true if this list contained the speci ed element

removeAll added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

boolean removeAll (Collection (https://developer.android.com/reference/java/util/Collection.html)<?> c)

Removes from this list all of its elements that are contained in the speci ed collection.

Parameters

c Collection: collection containing elements to be removed from this list

Returns

boolean true if this list changed as a result of the call

Throws

ClassCastException if the class of an element of this list is incompatible with the speci ed
(https://developer.android.com/reference/java/lang/ClassCastException.html) collection (optional
(https://developer.android.com/reference/java/util/Collection.html#optional-restrictions))

NullPointerException if this list contains a null element and the speci ed collection does not
(https://developer.android.com/reference/java/lang/NullPointerException.html) permit null elements (optional
(https://developer.android.com/reference/java/util/Collection.html#optional-restrictions)),

or if the speci ed collection is null

See also:

contains(Object) (https://developer.android.com/reference/java/util/Collection.html#contains(java.lang.Object))

removeIf added in API level 24 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

boolean removeIf (Predicate (https://developer.android.com/reference/java/util/function/Predicate.html)<? super E> filter)

Removes all of the elements of this collection that satisfy the given predicate. Errors or runtime exceptions thrown during iteration or by the predicate
are relayed to the caller.

Parameters

filter Predicate: a predicate which returns true for elements to be removed

https://developer.android.com/reference/java/util/ArrayList.html 12/18
11/24/2017 ArrayList | Android Developers
Returns

boolean true if any elements were removed

replaceAll added in API level 24 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

void replaceAll (UnaryOperator (https://developer.android.com/reference/java/util/function/UnaryOperator.html)<E> operator)

Replaces each element of this list with the result of applying the operator to that element. Errors or runtime exceptions thrown by the operator are
relayed to the caller.

Parameters

operator UnaryOperator: the operator to apply to each element

retainAll added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

boolean retainAll (Collection (https://developer.android.com/reference/java/util/Collection.html)<?> c)

Retains only the elements in this list that are contained in the speci ed collection. In other words, removes from this list all of its elements that are not
contained in the speci ed collection.

Parameters

c Collection: collection containing elements to be retained in this list

Returns

boolean true if this list changed as a result of the call

Throws

ClassCastException if the class of an element of this list is incompatible with the speci ed
(https://developer.android.com/reference/java/lang/ClassCastException.html) collection (optional
(https://developer.android.com/reference/java/util/Collection.html#optional-restrictions))

NullPointerException if this list contains a null element and the speci ed collection does not
(https://developer.android.com/reference/java/lang/NullPointerException.html) permit null elements (optional
(https://developer.android.com/reference/java/util/Collection.html#optional-restrictions)),

or if the speci ed collection is null

See also:

contains(Object) (https://developer.android.com/reference/java/util/Collection.html#contains(java.lang.Object))

https://developer.android.com/reference/java/util/ArrayList.html 13/18
11/24/2017 ArrayList | Android Developers

set added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

E set (int index,


E element)

Replaces the element at the speci ed position in this list with the speci ed element.

Parameters

index int: index of the element to replace

element E: element to be stored at the speci ed position

Returns

E the element previously at the speci ed position

Throws

IndexOutOfBoundsException
(https://developer.android.com/reference/java/lang/IndexOutOfBoundsException.html)

size added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

int size ()

Returns the number of elements in this list.

Returns

int the number of elements in this list

sort added in API level 24 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

void sort (Comparator (https://developer.android.com/reference/java/util/Comparator.html)<? super E> c)

Sorts this list according to the order induced by the speci ed Comparator (https://developer.android.com/reference/java/util/Comparator.html).

All elements in this list must be mutually comparable using the speci ed comparator (that is, c.compare(e1, e2) must not throw a ClassCastException
for any elements e1 and e2 in the list).

If the speci ed comparator is null then all elements in this list must implement the Comparable
(https://developer.android.com/reference/java/lang/Comparable.html) interface and the elements' natural ordering
(https://developer.android.com/reference/java/lang/Comparable.html) should be used.

This list must be modi able, but need not be resizable.

For apps running on and targeting Android versions greater than Nougat (API level > 25), sort(List)
(https://developer.android.com/reference/java/util/Collections.html#sort(java.util.List<T>)) delegates to this method. Such apps must not call sort(List)

https://developer.android.com/reference/java/util/ArrayList.html 14/18
11/24/2017 ArrayList | Android Developers
(https://developer.android.com/reference/java/util/Collections.html#sort(java.util.List<T>)) from this method. Instead, prefer not overriding this method at all.
If you must override it, consider this implementation:

@Override
public void sort(Comparator<? super E> c) {
Object[] elements = toArray();
Arrays.sort(elements, c);
ListIterator<E> iterator = (ListIterator<Object>) listIterator();
for (Object element : elements) {
iterator.next();
iterator.set((E) element);
}
}

Parameters

c Comparator: the Comparator used to compare list elements. A null value indicates that the elements' natural ordering
(https://developer.android.com/reference/java/lang/Comparable.html) should be used

spliterator added in API level 24 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

Spliterator (https://developer.android.com/reference/java/util/Spliterator.html)<E> spliterator ()

Creates a late-binding (https://developer.android.com/reference/java/util/Spliterator.html#binding) and fail-fast Spliterator


(https://developer.android.com/reference/java/util/Spliterator.html) over the elements in this list.

The Spliterator reports SIZED (https://developer.android.com/reference/java/util/Spliterator.html#SIZED), SUBSIZED


(https://developer.android.com/reference/java/util/Spliterator.html#SUBSIZED), and ORDERED
(https://developer.android.com/reference/java/util/Spliterator.html#ORDERED). Overriding implementations should document the reporting of additional
characteristic values.

Returns

Spliterator a Spliterator over the elements in this list


(https://developer.android.com/reference/java/util/Spliterator.html)<E>

subList added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

List (https://developer.android.com/reference/java/util/List.html)<E> subList (int fromIndex,


int toIndex)

Returns a view of the portion of this list between the speci ed fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the
returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are re ected in this list, and vice-versa. The
returned list supports all of the optional list operations.

https://developer.android.com/reference/java/util/ArrayList.html 15/18
11/24/2017 ArrayList | Android Developers
This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used
as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:

list.subList(from, to).clear();

Similar idioms may be constructed for indexOf(Object) (https://developer.android.com/reference/java/util/ArrayList.html#indexOf(java.lang.Object)) and


lastIndexOf(Object) (https://developer.android.com/reference/java/util/ArrayList.html#lastIndexOf(java.lang.Object)), and all of the algorithms in the
Collections (https://developer.android.com/reference/java/util/Collections.html) class can be applied to a subList.
The semantics of the list returned by this method become unde ned if the backing list (i.e., this list) is structurally modi ed in any way other than via the
returned list. (Structural modi cations are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may
yield incorrect results.)

Parameters

fromIndex int: low endpoint (inclusive) of the subList

toIndex int: high endpoint (exclusive) of the subList

Returns

List a view of the speci ed range within this list


(https://developer.android.com/reference/java/util/List.html)<E>

Throws

IndexOutOfBoundsException
(https://developer.android.com/reference/java/lang/IndexOutOfBoundsException.html)

IllegalArgumentException
(https://developer.android.com/reference/java/lang/IllegalArgumentException.html)

toArray added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

Object[] (https://developer.android.com/reference/java/lang/Object.html) toArray ()

Returns an array containing all of the elements in this list in proper sequence (from rst to last element).

The returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array). The caller
is thus free to modify the returned array.

This method acts as bridge between array-based and collection-based APIs.

Returns

Object[] an array containing all of the elements in this list in proper sequence
(https://developer.android.com/reference/java/lang/Object.html)

https://developer.android.com/reference/java/util/ArrayList.html 16/18
11/24/2017 ArrayList | Android Developers

toArray added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

T[] toArray (T[] a)

Returns an array containing all of the elements in this list in proper sequence (from rst to last element); the runtime type of the returned array is that of
the speci ed array. If the list ts in the speci ed array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the speci ed
array and the size of this list.

If the list ts in the speci ed array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the
end of the collection is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null
elements.)

Parameters

a T: the array into which the elements of the list are to be stored, if it is big enough; otherwise, a new array of the same runtime type is allocated
for this purpose.

Returns

T[] an array containing the elements of the list

Throws

ArrayStoreException if the runtime type of the speci ed array is not a supertype of the
(https://developer.android.com/reference/java/lang/ArrayStoreException.html) runtime type of every element in this list

NullPointerException if the speci ed array is null


(https://developer.android.com/reference/java/lang/NullPointerException.html)

trimToSize added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

void trimToSize ()

Trims the capacity of this ArrayList instance to be the list's current size. An application can use this operation to minimize the storage of an
ArrayList instance.

Protected methods

removeRange added in API level 1 (https://developer.android.com/guide/topics/manifest/uses-sdk-element.html#ApiLevels)

void removeRange (int fromIndex,


int toIndex)

Removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive. Shifts any succeeding elements to the
left (reduces their index). This call shortens the list by (toIndex - fromIndex) elements. (If toIndex==fromIndex, this operation has no effect.)

https://developer.android.com/reference/java/util/ArrayList.html 17/18
11/24/2017 ArrayList | Android Developers

Parameters

fromIndex int: index of rst element to be removed

toIndex int: index after last element to be removed

Throws

IndexOutOfBoundsException if fromIndex or toIndex is out of range (fromIndex < 0 ||


(https://developer.android.com/reference/java/lang/IndexOutOfBoundsException.html) fromIndex >= size() || toIndex > size() || toIndex <
fromIndex)

Follow @AndroidDev on Follow Android Developers on Check out Android Developers


Twitter Google+ on YouTube

https://developer.android.com/reference/java/util/ArrayList.html 18/18

You might also like