java.util

Class ArrayList<E>

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

An array-backed implementation of the List interface. This implements all optional list operations, and permits null elements, so that it is better than Vector, which it replaces. Random access is roughly constant time, and iteration is roughly linear time, so it is nice and fast, with less overhead than a LinkedList.

Each list has a capacity, and as the array reaches that capacity it is automatically transferred to a larger array. You also have access to ensureCapacity and trimToSize to control the backing array's size, avoiding reallocation or wasted memory.

ArrayList is not synchronized, so if you need multi-threaded access, consider using:
List l = Collections.synchronizedList(new ArrayList(...));

The iterators are fail-fast, meaning that any structural modification, except for remove() called on the iterator itself, cause the iterator to throw a {@link ConcurrentModificationException} rather than exhibit non-deterministic behavior.

See Also: Collection List LinkedList Vector synchronizedList AbstractList

UNKNOWN: updated to 1.4

Constructor Summary
ArrayList(int capacity)
Construct a new ArrayList with the supplied initial capacity.
ArrayList()
Construct a new ArrayList with the default capacity (16).
ArrayList(Collection<? extends E> c)
Construct a new ArrayList, and initialize it with the elements in the supplied Collection.
Method Summary
booleanadd(E e)
Appends the supplied element to the end of this list.
voidadd(int index, E e)
Adds the supplied element at the specified index, shifting all elements currently at that index or higher one to the right.
booleanaddAll(Collection<? extends E> c)
Add each element in the supplied Collection to this List.
booleanaddAll(int index, Collection<? extends E> c)
Add all elements in the supplied collection, inserting them beginning at the specified index. c can contain objects of any type, as well as null values.
voidclear()
Removes all elements from this List
Objectclone()
Creates a shallow copy of this ArrayList (elements are not cloned).
booleancontains(Object e)
Returns true iff element is in this ArrayList.
voidensureCapacity(int minCapacity)
Guarantees that this list will have at least enough capacity to hold minCapacity elements.
Eget(int index)
Retrieves the element at the user-supplied index.
intindexOf(Object e)
Returns the lowest index at which element appears in this List, or -1 if it does not appear.
booleanisEmpty()
Checks if the list is empty.
intlastIndexOf(Object e)
Returns the highest index at which element appears in this List, or -1 if it does not appear.
Eremove(int index)
Removes the element at the user-supplied index.
protected voidremoveRange(int fromIndex, int toIndex)
Removes all elements in the half-open interval [fromIndex, toIndex).
Eset(int index, E e)
Sets the element at the specified index.
intsize()
Returns the number of elements in this list.
Object[]toArray()
Returns an Object array containing all of the elements in this ArrayList.
<T> T[]toArray(T[] a)
Returns an Array whose component type is the runtime component type of the passed-in Array.
voidtrimToSize()
Trims the capacity of this List to be equal to its size; a memory saver.

Constructor Detail

ArrayList

public ArrayList(int capacity)
Construct a new ArrayList with the supplied initial capacity.

Parameters: capacity initial capacity of this ArrayList

Throws: IllegalArgumentException if capacity is negative

ArrayList

public ArrayList()
Construct a new ArrayList with the default capacity (16).

ArrayList

public ArrayList(Collection<? extends E> c)
Construct a new ArrayList, and initialize it with the elements in the supplied Collection. The initial capacity is 110% of the Collection's size.

Parameters: c the collection whose elements will initialize this list

Throws: NullPointerException if c is null

Method Detail

add

public boolean add(E e)
Appends the supplied element to the end of this list. The element, e, can be an object of any type or null.

Parameters: e the element to be appended to this list

Returns: true, the add will always succeed

add

public void add(int index, E e)
Adds the supplied element at the specified index, shifting all elements currently at that index or higher one to the right. The element, e, can be an object of any type or null.

Parameters: index the index at which the element is being added e the item being added

Throws: IndexOutOfBoundsException if index < 0 || index > size()

addAll

public boolean addAll(Collection<? extends E> c)
Add each element in the supplied Collection to this List. It is undefined what happens if you modify the list while this is taking place; for example, if the collection contains this list. c can contain objects of any type, as well as null values.

Parameters: c a Collection containing elements to be added to this List

Returns: true if the list was modified, in other words c is not empty

Throws: NullPointerException if c is null

addAll

public boolean addAll(int index, Collection<? extends E> c)
Add all elements in the supplied collection, inserting them beginning at the specified index. c can contain objects of any type, as well as null values.

Parameters: index the index at which the elements will be inserted c the Collection containing the elements to be inserted

Throws: IndexOutOfBoundsException if index < 0 || index > 0 NullPointerException if c is null

clear

public void clear()
Removes all elements from this List

clone

public Object clone()
Creates a shallow copy of this ArrayList (elements are not cloned).

Returns: the cloned object

contains

public boolean contains(Object e)
Returns true iff element is in this ArrayList.

Parameters: e the element whose inclusion in the List is being tested

Returns: true if the list contains e

ensureCapacity

public void ensureCapacity(int minCapacity)
Guarantees that this list will have at least enough capacity to hold minCapacity elements. This implementation will grow the list to max(current * 2, minCapacity) if (minCapacity > current). The JCL says explictly that "this method increases its capacity to minCap", while the JDK 1.3 online docs specify that the list will grow to at least the size specified.

Parameters: minCapacity the minimum guaranteed capacity

get

public E get(int index)
Retrieves the element at the user-supplied index.

Parameters: index the index of the element we are fetching

Throws: IndexOutOfBoundsException if index < 0 || index >= size()

indexOf

public int indexOf(Object e)
Returns the lowest index at which element appears in this List, or -1 if it does not appear.

Parameters: e the element whose inclusion in the List is being tested

Returns: the index where e was found

isEmpty

public boolean isEmpty()
Checks if the list is empty.

Returns: true if there are no elements

lastIndexOf

public int lastIndexOf(Object e)
Returns the highest index at which element appears in this List, or -1 if it does not appear.

Parameters: e the element whose inclusion in the List is being tested

Returns: the index where e was found

remove

public E remove(int index)
Removes the element at the user-supplied index.

Parameters: index the index of the element to be removed

Returns: the removed Object

Throws: IndexOutOfBoundsException if index < 0 || index >= size()

removeRange

protected void removeRange(int fromIndex, int toIndex)
Removes all elements in the half-open interval [fromIndex, toIndex). Does nothing when toIndex is equal to fromIndex.

Parameters: fromIndex the first index which will be removed toIndex one greater than the last index which will be removed

Throws: IndexOutOfBoundsException if fromIndex > toIndex

set

public E set(int index, E e)
Sets the element at the specified index. The new element, e, can be an object of any type or null.

Parameters: index the index at which the element is being set e the element to be set

Returns: the element previously at the specified index

Throws: IndexOutOfBoundsException if index < 0 || index >= 0

size

public int size()
Returns the number of elements in this list.

Returns: the list size

toArray

public Object[] toArray()
Returns an Object array containing all of the elements in this ArrayList. The array is independent of this list.

Returns: an array representation of this list

toArray

public <T> T[] toArray(T[] a)
Returns an Array whose component type is the runtime component type of the passed-in Array. The returned Array is populated with all of the elements in this ArrayList. If the passed-in Array is not large enough to store all of the elements in this List, a new Array will be created and returned; if the passed-in Array is larger than the size of this List, then size() index will be set to null.

Parameters: a the passed-in Array

Returns: an array representation of this list

Throws: ArrayStoreException if the runtime type of a does not allow an element in this list NullPointerException if a is null

trimToSize

public void trimToSize()
Trims the capacity of this List to be equal to its size; a memory saver.