Class Overview
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
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.
|
[Expand]
Inherited Methods |
From class java.util.AbstractList
boolean
|
add(E object)
Adds the specified object at the end of this List.
|
void
|
add(int location, E object)
Inserts the specified object into this List at the specified location.
|
boolean
|
addAll(int location, 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 list, leaving it empty.
|
boolean
|
equals(Object object)
Compares the specified object to this list and return true if they are
equal.
|
abstract
E
|
get(int location)
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.
|
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.
|
ListIterator<E>
|
listIterator(int location)
Returns a list iterator on the elements of this list.
|
ListIterator<E>
|
listIterator()
Returns a ListIterator on the elements of this list.
|
E
|
remove(int location)
Removes the object at the specified location from this list.
|
void
|
removeRange(int start, int end)
Removes the objects in the specified range from the start to the end
index minus one.
|
E
|
set(int location, E object)
Replaces the element at the specified location in this list with the
specified object.
|
List<E>
|
subList(int start, int end)
Returns a part of consecutive elements of this list as a view.
|
|
From class java.util.AbstractCollection
boolean
|
add(E object)
Attempts to add object to the contents of this
Collection (optional).
|
boolean
|
addAll(Collection<? extends E> collection)
Attempts to add all of the objects contained in collection
to the contents of this Collection (optional).
|
void
|
clear()
Removes all elements from this Collection , leaving it empty (optional).
|
boolean
|
contains(Object object)
Tests whether this Collection contains the specified object.
|
boolean
|
containsAll(Collection<?> collection)
Tests whether this Collection contains all objects contained in the
specified Collection .
|
boolean
|
isEmpty()
Returns if this Collection contains no elements.
|
abstract
Iterator<E>
|
iterator()
Returns an instance of Iterator that may be used to access the
objects contained by this Collection .
|
boolean
|
remove(Object object)
Removes one instance of the specified object from this Collection if one
is contained (optional).
|
boolean
|
removeAll(Collection<?> collection)
Removes all occurrences in this Collection of each object in the
specified Collection (optional).
|
boolean
|
retainAll(Collection<?> collection)
Removes all objects from this Collection that are not also found in the
Collection passed (optional).
|
abstract
int
|
size()
Returns a count of how many objects this Collection contains.
|
<T>
T[]
|
toArray(T[] contents)
Returns an array containing all elements contained in this Collection .
|
Object[]
|
toArray()
Returns a new array containing all elements contained in this Collection .
|
String
|
toString()
Returns the string representation of this Collection .
|
|
From class java.lang.Object
Object
|
clone()
Creates and returns a copy of this Object .
|
boolean
|
equals(Object o)
Compares this instance with the specified object and indicates if they
are equal.
|
void
|
finalize()
Called before the object's memory is reclaimed by the VM.
|
final
Class<? extends Object>
|
getClass()
Returns the unique instance of Class that represents this
object's class.
|
int
|
hashCode()
Returns an integer hash code for this object.
|
final
void
|
notify()
Causes a thread which is waiting on this object's monitor (by means of
calling one of the wait() methods) to be woken up.
|
final
void
|
notifyAll()
Causes all threads which are waiting on this object's monitor (by means
of calling one of the wait() methods) to be woken up.
|
String
|
toString()
Returns a string containing a concise, human-readable description of this
object.
|
final
void
|
wait()
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.
|
final
void
|
wait(long millis, int nanos)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait(long millis)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
|
From interface java.lang.Iterable
|
From interface java.util.Collection
abstract
boolean
|
add(E object)
Attempts to add object to the contents of this
Collection (optional).
|
abstract
boolean
|
addAll(Collection<? extends E> collection)
Attempts to add all of the objects contained in Collection
to the contents of this Collection (optional).
|
abstract
void
|
clear()
Removes all elements from this Collection , leaving it empty (optional).
|
abstract
boolean
|
contains(Object object)
Tests whether this Collection contains the specified object.
|
abstract
boolean
|
containsAll(Collection<?> collection)
Tests whether this Collection contains all objects contained in the
specified Collection .
|
abstract
boolean
|
equals(Object object)
Compares the argument to the receiver, and returns true if they represent
the same object using a class specific comparison.
|
abstract
int
|
hashCode()
Returns an integer hash code for the receiver.
|
abstract
boolean
|
isEmpty()
Returns if this Collection contains no elements.
|
abstract
Iterator<E>
|
iterator()
Returns an instance of Iterator that may be used to access the
objects contained by this Collection .
|
abstract
boolean
|
remove(Object object)
Removes one instance of the specified object from this Collection if one
is contained (optional).
|
abstract
boolean
|
removeAll(Collection<?> collection)
Removes all occurrences in this Collection of each object in the
specified Collection (optional).
|
abstract
boolean
|
retainAll(Collection<?> collection)
Removes all objects from this Collection that are not also found in the
Collection passed (optional).
|
abstract
int
|
size()
Returns a count of how many objects this Collection contains.
|
abstract
<T>
T[]
|
toArray(T[] array)
Returns an array containing all elements contained in this Collection .
|
abstract
Object[]
|
toArray()
Returns a new array containing all elements contained in this Collection .
|
|
From interface java.util.List
abstract
boolean
|
add(E object)
Adds the specified object at the end of this List .
|
abstract
void
|
add(int location, E object)
Inserts the specified object into this List at the specified location.
|
abstract
boolean
|
addAll(Collection<? extends E> collection)
Adds the objects in the specified collection to the end of this List .
|
abstract
boolean
|
addAll(int location, Collection<? extends E> collection)
Inserts the objects in the specified collection at the specified location
in this List .
|
abstract
void
|
clear()
Removes all elements from this List , leaving it empty.
|
abstract
boolean
|
contains(Object object)
Tests whether this List contains the specified object.
|
abstract
boolean
|
containsAll(Collection<?> collection)
Tests whether this List contains all objects contained in the
specified collection.
|
abstract
boolean
|
equals(Object object)
Compares the given object with the List , and returns true if they
represent the same object using a class specific comparison.
|
abstract
E
|
get(int location)
Returns the element at the specified location in this List .
|
abstract
int
|
hashCode()
Returns the hash code for this List .
|
abstract
int
|
indexOf(Object object)
Searches this List for the specified object and returns the index of the
first occurrence.
|
abstract
boolean
|
isEmpty()
Returns whether this List contains no elements.
|
abstract
Iterator<E>
|
iterator()
Returns an iterator on the elements of this List .
|
abstract
int
|
lastIndexOf(Object object)
Searches this List for the specified object and returns the index of the
last occurrence.
|
abstract
ListIterator<E>
|
listIterator(int location)
Returns a list iterator on the elements of this List .
|
abstract
ListIterator<E>
|
listIterator()
Returns a List iterator on the elements of this List .
|
abstract
E
|
remove(int location)
Removes the object at the specified location from this List .
|
abstract
boolean
|
remove(Object object)
Removes the first occurrence of the specified object from this List .
|
abstract
boolean
|
removeAll(Collection<?> collection)
Removes all occurrences in this List of each object in the specified
collection.
|
abstract
boolean
|
retainAll(Collection<?> collection)
Removes all objects from this List that are not contained in the
specified collection.
|
abstract
E
|
set(int location, E object)
Replaces the element at the specified location in this List with the
specified object.
|
abstract
int
|
size()
Returns the number of elements in this List .
|
abstract
List<E>
|
subList(int start, int end)
Returns a List of the specified portion of this List from the given start
index to the end index minus one.
|
abstract
<T>
T[]
|
toArray(T[] array)
Returns an array containing all elements contained in this List .
|
abstract
Object[]
|
toArray()
Returns an array containing all elements contained in this List .
|
|
Public Constructors
public
ArrayList
(int capacity)
Constructs a new instance of ArrayList
with the specified
initial capacity.
Parameters
capacity
| the initial capacity of this ArrayList .
|
public
ArrayList
()
Constructs a new ArrayList
instance with zero initial capacity.
public
ArrayList
(Collection<? extends E> collection)
Constructs a new instance of ArrayList
containing the elements of
the specified collection.
Parameters
collection
| the collection of elements to add.
|
Public Methods
public
boolean
add
(E object)
Adds the specified object at the end of this ArrayList
.
Parameters
object
| the object to add. |
public
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
| the index at which to insert the object. |
object
| the object to add. |
public
boolean
addAll
(Collection<? extends E> collection)
Adds the objects in the specified collection to this ArrayList
.
Parameters
collection
| the collection of objects. |
Returns
true
if this ArrayList
is modified, false
otherwise.
public
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
| the index at which to insert. |
collection
| the collection of objects. |
Returns
true
if this ArrayList
is modified, false
otherwise.
public
void
clear
()
Removes all elements from this ArrayList
, leaving it empty.
public
Object
clone
()
Returns a new ArrayList
with the same elements, the same size and
the same capacity as this ArrayList
.
Returns
- a shallow copy of this
ArrayList
public
boolean
contains
(Object object)
Searches this ArrayList
for the specified object.
Parameters
object
| the object to search for. |
Returns
true
if object
is an element of this
ArrayList
, false
otherwise
public
void
ensureCapacity
(int minimumCapacity)
Ensures that after this operation the ArrayList
can hold the
specified number of elements without further growing.
Parameters
minimumCapacity
| the minimum capacity asked for.
|
public
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
| the object to compare to this object. |
Returns
true
if the specified object is equal to this list,
false
otherwise.
public
E
get
(int index)
Returns the element at the specified location in this list.
Parameters
index
| the index of the element to return. |
Returns
- the element at the specified index.
public
int
hashCode
()
Returns the hash code of this list. The hash code is calculated by taking
each element's hashcode into account.
public
int
indexOf
(Object object)
Searches this list for the specified object and returns the index of the
first occurrence.
Parameters
object
| the object to search for. |
Returns
- the index of the first occurrence of the object, or -1 if it was
not found.
public
boolean
isEmpty
()
Returns if this Collection
contains no elements. This implementation
tests, whether size
returns 0.
Returns
true
if this Collection
has no elements, false
otherwise.
public
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
- an iterator on the elements of this list.
public
int
lastIndexOf
(Object object)
Searches this list for the specified object and returns the index of the
last occurrence.
Parameters
object
| the object to search for. |
Returns
- the index of the last occurrence of the object, or -1 if the
object was not found.
public
E
remove
(int index)
Removes the object at the specified location from this list.
Parameters
index
| the index of the object to remove. |
public
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
| the object to remove. |
Returns
true
if this Collection
is modified, false
otherwise.
public
E
set
(int index, E object)
Replaces the element at the specified location in this ArrayList
with the specified object.
Parameters
index
| the index at which to put the specified object. |
object
| the object to add. |
Returns
- the previous element at the index.
public
int
size
()
Returns the number of elements in this ArrayList
.
Returns
- the number of elements in this
ArrayList
.
public
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.
Returns
- 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.
|
public
Object[]
toArray
()
Returns a new array containing all elements contained in this
ArrayList
.
Returns
- an array of the elements from this
ArrayList
public
void
trimToSize
()
Sets the capacity of this ArrayList
to be the same as the current
size.
Protected Methods
protected
void
removeRange
(int fromIndex, int toIndex)
Removes the objects in the specified range from the start to the end
index minus one.
Parameters
fromIndex
| the index at which to start removing. |
toIndex
| the index after the last element to remove. |