This
class is a List implementation based on an array
(that is recreated as necessary as the list grows or shrinks).
ArrayList implements all optional
List and Collection methods and
allows list elements of any type (including null).
Because ArrayList is based on an array, the
get( ) and set( ) methods are
very efficient. (This is not the case for the
LinkedList implementation, for example.)
ArrayList is a general-purpose implementation of
List and is quite commonly used.
ArrayList is very much like the
Vector class, except that its methods are not
synchronized. If you are using an ArrayList in a
multithreaded environment, you should explicitly synchronize any
modifications to the list, or wrap the list with
Collections.synchronizedList( ). See
List and Collection for details
on the methods of ArrayList. See also
LinkedList.
An ArrayList has
a
capacity , which is the number of elements in
the internal array that contains the elements of the list. When the
number of elements exceeds the capacity, a new array, with a larger
capacity, must be created. In addition to the List
and Collection methods,
ArrayList defines a couple of methods that help
you manage this capacity. If you know in advance how many elements an
ArrayList will contain, you can call
ensureCapacity( ), which can increase efficiency
by avoiding incremental reallocation of the internal array. You can
also pass an initial capacity value to the ArrayList(
) constructor. Finally, if an ArrayList
has reached its final size and will not change in the future, you can
call TRimToSize( ) to reallocate the internal
array with a capacity that matches the list size exactly. When the
ArrayList will have a long lifetime, this can be a
useful technique to reduce memory usage.
public class
ArrayList<E> extends AbstractList<E> implements List<E>,
RandomAccess, Cloneable, Serializable {
// Public Constructors
public
ArrayList ( );
public
ArrayList (int
initialCapacity );
public
ArrayList (Collection<? extends E>
c );
// Public Instance Methods
public void
ensureCapacity (int
minCapacity );
public void
trimToSize ( );
// Methods Implementing List
public boolean
add (E
o );
public void
add (int
index , E
element );
public boolean
addAll (Collection<? extends E>
c );
public boolean
addAll (int
index , Collection<? extends E>
c );
public void
clear ( );
public boolean
contains (Object
elem );
public E
get (int
index );
public int
indexOf (Object
elem );
public boolean
isEmpty ( ); default:true
public int
lastIndexOf (Object
elem );
5.0 public boolean
remove (Object
o );
public E
remove (int
index );
public E
set (int
index , E
element );
public int
size ( );
public Object[ ]
toArray ( );
public <T> T[ ]
toArray (T[ ]
a );
// Protected Methods Overriding AbstractList
protected void
removeRange (int
fromIndex , int
toIndex );
// Public Methods Overriding Object
public Object
clone ( );
}