public class java.util.ArrayList
[source file: ArrayList.java]java.lang.Object | +----java.util.AbstractCollection | +----java.util.AbstractList | +----java.util.ArrayList The pure class interface.public class ArrayList extends AbstractList implements List, Cloneable, java.io.Serializable 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, ArrayList provides methods to manipulate the size of the array that is used internally to store the List. [ArrayList is roughly equivalent to Vector, except that it is unsynchronized.]
The size, isEmpty, get, set, clear, iterator, and listIterator operations run in constant time. The add[] operation runs in constant time unless it causes the ArrayList to exceed its capacity, in which case it runs in linear time. All of the other operations run in linear time [roughly speaking]. The constant factor is low compared to that for LinkedList.
Each ArrayList has a capacity and a capacityIncrement. 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; it is usually larger because as components are added to the ArrayList, the ArrayList's storage increases in chunks the size of its capacityIncrement. [The capacityIncrement does not change over the lifetime of the ArrayList.] An application can increase the capacity of an ArrayList before inserting a large number of components; this reduces the amount of incremental reallocation.
Note that this implementation is not synchronized. If multiple threads access an ArrayList concurrently, and at least one of the threads modifies the ArrayList structurally, it must be synchronized externally. [A structural modification 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 modification.] This is typically accomplished by synchronizing on some object that naturally encapsulates the ArrayList. If no such object exists, the ArrayList should be "wrapped" using the Collections.synchronizedSet method. This is best done at creation time, to prevent accidental unsynchronized access to the ArrayList:
List list = Collections.synchronizedList[new ArrayList[...]];The Iterators returned by ArrayList's iterator and listIterator methods are fail-fast: if the ArrayList is structurally modified at any time after the Iterator is created, in any way except through the Iterator's own remove or add methods, the Iterator will throw a ConcurrentModificationException. Thus, in the face of concurrent modification, the Iterator fails quickly and cleanly, rather than risking arbitrary, non-deterministic behavior at an undetermined time in the future.
See also:Collection, List, LinkedList, Vector, Collections.synchronizedList ArrayList[]Constructs an empty ArrayList. ArrayList[Collection]Constructs an ArrayList containing the elements of the specified Collection, in the order ArrayList[int]Constructs an empty ArrayList with the specified initial capacity. ArrayList[int, int]Constructs an empty ArrayList with the specified initial capacity and capacity add[int, A]Inserts the specified element at the specified position in this ArrayList add[A]Appends the specified element to the end of this ArrayList. addAll[int, Collection]Inserts all of the elements in the specified Collection into this ArrayList, starting at addAll[Collection]Appends all of the elements in the specified Collection to the end of this this clear[]Removes all of the elements from this ArrayList clone[]Returns a shallow copy of this ArrayList contains[A]Returns true if this ArrayList contains the specified element. ensureCapacity[int]Increases the capacity of this ArrayList, if necessary, to ensure that it can hold at get[int]Returns the element at the specified position in this ArrayList. indexOf[A, int]Searches for the first occurence of the given argument, beginning the search at indexOf[A]Searches for the first occurence of the given argument, testing for equality using the isEmpty[]Tests if this ArrayList has no components. lastIndexOf[A, int]Searches backwards for the specified object, starting from the specified index, and lastIndexOf[A]Returns the index of the last occurrence of the specified object in this ArrayList. remove[int]Removes the element at the specified position in this ArrayList. Shifts any subsequent removeRange[int, int]Removes from this ArrayList all of the elements whose index is between fromIndex, set[int, A]Replaces the element at the specified position in this ArrayList with the specified size[]Returns the number of components in this ArrayList. toArray[]Returns an array containing all of the elements in this ArrayList in the correct order. trimToSize[]Trims the capacity of this ArrayList to be the ArrayList's current size ArrayListpublic ArrayList[int initialCapacity, int capacityIncrement]; Constructs an empty ArrayList with the specified initial capacity and capacity increment.Parameters:initialCapacity - the initial capacity of the ArrayList.capacityIncrement - the amount by which the capacity is increased when the ArrayList overflows.
ArrayList
ArrayList
ArrayList
public void trimToSize[]; Trims the capacity of this ArrayList to be the ArrayList's current size. An application can use this operation to minimize the storage of an ArrayList.
ensureCapacity
size
isEmpty
contains
indexOf
indexOf
lastIndexOf
lastIndexOf
clone
toArray
get
set
add
add
remove
clear
addAll
removeRange
addAll