Skip to content

Most visited

Recently visited

navigation
Added in API level 1

ArrayList

public class ArrayList
extends AbstractList<E> implements Cloneable, Serializable, RandomAccess

java.lang.Object
   ↳ java.util.AbstractCollection<E>
     ↳ java.util.AbstractList<E>
       ↳ java.util.ArrayList<E>
Known Direct Subclasses


ArrayList is an implementation of List, backed by an array. All optional operations including adding, removing, and replacing elements are supported.

All elements are permitted, including null.

This class is a good choice as your default List implementation. Vector synchronizes all operations, but not necessarily in a way that's meaningful to your application: synchronizing each call to get, for example, is not equivalent to synchronizing the list and iterating over it (which is probably what you intended). CopyOnWriteArrayList is intended for the special case of very high concurrency, frequent traversals, and very rare mutations.

Summary

Inherited fields

From class java.util.AbstractList

Public constructors

ArrayList(int capacity)

Constructs a new instance of ArrayList with the specified initial capacity.

ArrayList()

Constructs a new ArrayList instance with zero initial capacity.

ArrayList(Collection<? extends E> collection)

Constructs a new instance of ArrayList containing the elements of the specified collection.

Public methods

boolean add(E object)

Adds the specified object at the end of this ArrayList.

void add(int index, E object)

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

boolean addAll(Collection<? extends E> collection)

Adds the objects in the specified collection to this ArrayList.

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

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

void clear()

Removes all elements from this ArrayList, leaving it empty.

Object clone()

Returns a new ArrayList with the same elements, the same size and the same capacity as this ArrayList.

boolean contains(Object object)

Searches this ArrayList for the specified object.

void ensureCapacity(int minimumCapacity)

Ensures that after this operation the ArrayList can hold the specified number of elements without further growing.

boolean equals(Object o)

Compares the specified object to this list and return true if they are equal.

E get(int index)

Returns the element at the specified location in this list.

int hashCode()

Returns the hash code of this list.

int indexOf(Object object)

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

boolean isEmpty()

Returns if this Collection contains no elements.

Iterator<E> iterator()

Returns an iterator on the elements of this list.

int lastIndexOf(Object object)

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

E remove(int index)

Removes the object at the specified location from this list.

boolean remove(Object object)

Removes one instance of the specified object from this Collection if one is contained (optional).

E set(int index, E object)

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

int size()

Returns the number of elements in this ArrayList.

<T> T[] toArray(T[] contents)

Returns an array containing all elements contained in this ArrayList.

Object[] toArray()

Returns a new array containing all elements contained in this ArrayList.

void trimToSize()

Sets the capacity of this ArrayList to be the same as the current size.

Protected methods

void removeRange(int fromIndex, int toIndex)

Removes the objects in the specified range from the start to the end index minus one.

Inherited methods

From class java.util.AbstractList
From class java.util.AbstractCollection
From class java.lang.Object
From interface java.util.List
From interface java.util.Collection
From interface java.lang.Iterable

Public constructors

ArrayList

Added in API level 1
ArrayList (int capacity)

Constructs a new instance of ArrayList with the specified initial capacity.

Parameters
capacity int: the initial capacity of this ArrayList.

ArrayList

Added in API level 1
ArrayList ()

Constructs a new ArrayList instance with zero initial capacity.

ArrayList

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

Constructs a new instance of ArrayList containing the elements of the specified collection.

Parameters
collection Collection: the collection of elements to add.

Public methods

add

Added in API level 1
boolean add (E object)

Adds the specified object at the end of this ArrayList.

Parameters
object E: the object to add.
Returns
boolean always true

add

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

Inserts the specified object into this ArrayList at the specified location. The object is inserted before any previous element at the specified location. If the location is equal to the size of this ArrayList, the object is added at the end.

Parameters
index int: the index at which to insert the object.
object E: the object to add.
Throws
IndexOutOfBoundsException when location < 0 || location > size()

addAll

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

Adds the objects in the specified collection to this ArrayList.

Parameters
collection Collection: the collection of objects.
Returns
boolean true if this ArrayList is modified, false otherwise.

addAll

Added in API level 1
boolean addAll (int index, 
                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
index int: the index at which to insert.
collection Collection: the collection of objects.
Returns
boolean true if this ArrayList is modified, false otherwise.
Throws
IndexOutOfBoundsException when location < 0 || location > size()

clear

Added in API level 1
void clear ()

Removes all elements from this ArrayList, leaving it empty.

See also:

clone

Added in API level 1
Object clone ()

Returns a new ArrayList with the same elements, the same size and the same capacity as this ArrayList.

Returns
Object a shallow copy of this ArrayList

See also:

contains

Added in API level 1
boolean contains (Object object)

Searches this ArrayList for the specified object.

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

ensureCapacity

Added in API level 1
void ensureCapacity (int minimumCapacity)

Ensures that after this operation the ArrayList can hold the specified number of elements without further growing.

Parameters
minimumCapacity int: the minimum capacity asked for.

equals

Added in API level 1
boolean equals (Object o)

Compares the specified object to this list and return true if they are equal. Two lists are equal when they both contain the same objects in the same order.

Parameters
o Object: the object to compare to this object.
Returns
boolean true if the specified object is equal to this list, false otherwise.

get

Added in API level 1
E get (int index)

Returns the element at the specified location in this list.

Parameters
index int: the index of the element to return.
Returns
E the element at the specified index.

hashCode

Added in API level 1
int hashCode ()

Returns the hash code of this list. The hash code is calculated by taking each element's hashcode into account.

Returns
int the hash code.

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 it was not found.

isEmpty

Added in API level 1
boolean isEmpty ()

Returns if this Collection contains no elements. This implementation tests, whether size returns 0.

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

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.

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.

remove

Added in API level 1
E remove (int index)

Removes the object at the specified location from this list.

Parameters
index int: the index of the object to remove.
Returns
E the removed object.
Throws
IndexOutOfBoundsException when location < 0 || location >= size()

remove

Added in API level 1
boolean remove (Object object)

Removes one instance of the specified object from this Collection if one is contained (optional). This implementation iterates over this Collection and tests for each element e returned by the iterator, whether e is equal to the given object. If object != null then this test is performed using object.equals(e), otherwise using object == null. If an element equal to the given object is found, then the remove method is called on the iterator and true is returned, false otherwise. If the iterator does not support removing elements, an UnsupportedOperationException is thrown.

Parameters
object Object: the object to remove.
Returns
boolean true if this Collection is modified, false otherwise.

set

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

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

Parameters
index int: the index at which to put the specified object.
object E: the object to add.
Returns
E the previous element at the index.
Throws
IndexOutOfBoundsException when location < 0 || location >= size()

size

Added in API level 1
int size ()

Returns the number of elements in this ArrayList.

Returns
int the number of elements in this ArrayList.

toArray

Added in API level 1
T[] toArray (T[] contents)

Returns an array containing all elements contained in this ArrayList. 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 ArrayList, the array element following the collection elements is set to null.

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

toArray

Added in API level 1
Object[] toArray ()

Returns a new array containing all elements contained in this ArrayList.

Returns
Object[] an array of the elements from this ArrayList

trimToSize

Added in API level 1
void trimToSize ()

Sets the capacity of this ArrayList to be the same as the current size.

See also:

Protected methods

removeRange

Added in API level 1
void removeRange (int fromIndex, 
                int toIndex)

Removes the objects in the specified range from the start to the end index minus one.

Parameters
fromIndex int: the index at which to start removing.
toIndex int: the index after the last element to remove.
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.