Package java.util
Class ArrayList<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.ArrayList<E>
- Type Parameters:
E- The element type of this list.
- All Implemented Interfaces:
Serializable,Cloneable,Iterable<E>,Collection<E>,List<E>,RandomAccess
public class ArrayList<E> extends AbstractList<E> implements Cloneable, Serializable, RandomAccess
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.
- Since:
- 1.2
- See Also:
- Serialized Form
-
Field Summary
Fields inherited from class java.util.AbstractList
modCount -
Constructor Summary
Constructors Constructor Description ArrayList()Constructs a newArrayListinstance with zero initial capacity.ArrayList(int capacity)Constructs a new instance ofArrayListwith the specified initial capacity.ArrayList(Collection<? extends E> collection)Constructs a new instance ofArrayListcontaining the elements of the specified collection. -
Method Summary
Modifier and Type Method Description voidadd(int index, E object)Inserts the specified object into thisArrayListat the specified location.booleanadd(E object)Adds the specified object at the end of thisArrayList.booleanaddAll(int index, Collection<? extends E> collection)Inserts the objects in the specified collection at the specified location in this List.booleanaddAll(Collection<? extends E> collection)Adds the objects in the specified collection to thisArrayList.voidclear()Removes all elements from thisArrayList, leaving it empty.Objectclone()Returns a newArrayListwith the same elements, the same size and the same capacity as thisArrayList.booleancontains(Object object)Searches thisArrayListfor the specified object.voidensureCapacity(int minimumCapacity)Ensures that after this operation theArrayListcan hold the specified number of elements without further growing.booleanequals(Object o)Compares the specified object to this list and return true if they are equal.Eget(int index)Returns the element at the specified location in this list.inthashCode()Returns the hash code of this list.intindexOf(Object object)Searches this list for the specified object and returns the index of the first occurrence.booleanisEmpty()Returns if thisCollectioncontains no elements.Iterator<E>iterator()Returns an iterator on the elements of this list.intlastIndexOf(Object object)Searches this list for the specified object and returns the index of the last occurrence.Eremove(int index)Removes the object at the specified location from this list.booleanremove(Object object)Removes one instance of the specified object from thisCollectionif one is contained (optional).protected voidremoveRange(int fromIndex, int toIndex)Removes the objects in the specified range from the start to the end index minus one.Eset(int index, E object)Replaces the element at the specified location in thisArrayListwith the specified object.intsize()Returns the number of elements in thisArrayList.Object[]toArray()Returns a new array containing all elements contained in thisArrayList.<T> T[]toArray(T[] contents)Returns an array containing all elements contained in thisArrayList.voidtrimToSize()Sets the capacity of thisArrayListto be the same as the current size.Methods inherited from class java.util.AbstractList
listIterator, listIterator, subListMethods inherited from class java.util.AbstractCollection
containsAll, removeAll, retainAll, toStringMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.List
containsAll, removeAll, retainAll
-
Constructor Details
-
ArrayList
public ArrayList(int capacity)Constructs a new instance ofArrayListwith the specified initial capacity.- Parameters:
capacity- the initial capacity of thisArrayList.
-
ArrayList
public ArrayList()Constructs a newArrayListinstance with zero initial capacity. -
ArrayList
Constructs a new instance ofArrayListcontaining the elements of the specified collection.- Parameters:
collection- the collection of elements to add.
-
-
Method Details
-
add
Adds the specified object at the end of thisArrayList.- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceList<E>- Overrides:
addin classAbstractList<E>- Parameters:
object- the object to add.- Returns:
- always true
-
add
Inserts the specified object into thisArrayListat the specified location. The object is inserted before any previous element at the specified location. If the location is equal to the size of thisArrayList, the object is added at the end.- Specified by:
addin interfaceList<E>- Overrides:
addin classAbstractList<E>- Parameters:
index- the index at which to insert the object.object- the object to add.- Throws:
IndexOutOfBoundsException- whenlocation < 0 || location > size()
-
addAll
Adds the objects in the specified collection to thisArrayList.- Specified by:
addAllin interfaceCollection<E>- Specified by:
addAllin interfaceList<E>- Overrides:
addAllin classAbstractCollection<E>- Parameters:
collection- the collection of objects.- Returns:
trueif thisArrayListis modified,falseotherwise.
-
addAll
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.- Specified by:
addAllin interfaceList<E>- Overrides:
addAllin classAbstractList<E>- Parameters:
index- the index at which to insert.collection- the collection of objects.- Returns:
trueif thisArrayListis modified,falseotherwise.- Throws:
IndexOutOfBoundsException- whenlocation < 0 || location > size()
-
clear
public void clear()Removes all elements from thisArrayList, leaving it empty.- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceList<E>- Overrides:
clearin classAbstractList<E>- See Also:
isEmpty(),size
-
clone
Returns a newArrayListwith the same elements, the same size and the same capacity as thisArrayList. -
ensureCapacity
public void ensureCapacity(int minimumCapacity)Ensures that after this operation theArrayListcan hold the specified number of elements without further growing.- Parameters:
minimumCapacity- the minimum capacity asked for.
-
get
Description copied from class:AbstractListReturns the element at the specified location in this list. -
size
public int size()Returns the number of elements in thisArrayList.- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceList<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- the number of elements in this
ArrayList.
-
isEmpty
public boolean isEmpty()Description copied from class:AbstractCollectionReturns if thisCollectioncontains no elements. This implementation tests, whethersizereturns 0.- Specified by:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceList<E>- Overrides:
isEmptyin classAbstractCollection<E>- Returns:
trueif thisCollectionhas no elements,falseotherwise.- See Also:
AbstractCollection.size()
-
contains
Searches thisArrayListfor the specified object.- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceList<E>- Overrides:
containsin classAbstractCollection<E>- Parameters:
object- the object to search for.- Returns:
trueifobjectis an element of thisArrayList,falseotherwise
-
indexOf
Description copied from class:AbstractListSearches this list for the specified object and returns the index of the first occurrence. -
lastIndexOf
Description copied from class:AbstractListSearches this list for the specified object and returns the index of the last occurrence.- Specified by:
lastIndexOfin interfaceList<E>- Overrides:
lastIndexOfin classAbstractList<E>- 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.
-
remove
Removes the object at the specified location from this list.- Specified by:
removein interfaceList<E>- Overrides:
removein classAbstractList<E>- Parameters:
index- the index of the object to remove.- Returns:
- the removed object.
- Throws:
IndexOutOfBoundsException- whenlocation < 0 || location >= size()
-
remove
Description copied from class:AbstractCollectionRemoves one instance of the specified object from thisCollectionif one is contained (optional). This implementation iterates over thisCollectionand tests for each elementereturned by the iterator, whethereis equal to the given object. Ifobject != nullthen this test is performed usingobject.equals(e), otherwise usingobject == null. If an element equal to the given object is found, then theremovemethod is called on the iterator andtrueis returned,falseotherwise. If the iterator does not support removing elements, anUnsupportedOperationExceptionis thrown.- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceList<E>- Overrides:
removein classAbstractCollection<E>- Parameters:
object- the object to remove.- Returns:
trueif thisCollectionis modified,falseotherwise.
-
removeRange
protected void removeRange(int fromIndex, int toIndex)Description copied from class:AbstractListRemoves the objects in the specified range from the start to the end index minus one.- Overrides:
removeRangein classAbstractList<E>- Parameters:
fromIndex- the index at which to start removing.toIndex- the index after the last element to remove.
-
set
Replaces the element at the specified location in thisArrayListwith the specified object.- Specified by:
setin interfaceList<E>- Overrides:
setin classAbstractList<E>- Parameters:
index- the index at which to put the specified object.object- the object to add.- Returns:
- the previous element at the index.
- Throws:
IndexOutOfBoundsException- whenlocation < 0 || location >= size()
-
toArray
Returns a new array containing all elements contained in thisArrayList.- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceList<E>- Overrides:
toArrayin classAbstractCollection<E>- Returns:
- an array of the elements from this
ArrayList
-
toArray
public <T> T[] toArray(T[] contents)Returns an array containing all elements contained in thisArrayList. 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 thisArrayList, the array element following the collection elements is set to null.- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceList<E>- Overrides:
toArrayin classAbstractCollection<E>- Parameters:
contents- the array.- Returns:
- an array of the elements from this
ArrayList. - Throws:
ArrayStoreException- when the type of an element in thisArrayListcannot be stored in the type of the specified array.
-
trimToSize
public void trimToSize()Sets the capacity of thisArrayListto be the same as the current size.- See Also:
size
-
iterator
Description copied from class:AbstractListReturns an iterator on the elements of this list. The elements are iterated in the same order as they occur in the list. -
hashCode
public int hashCode()Description copied from class:AbstractListReturns the hash code of this list. The hash code is calculated by taking each element's hashcode into account.- Specified by:
hashCodein interfaceCollection<E>- Specified by:
hashCodein interfaceList<E>- Overrides:
hashCodein classAbstractList<E>- Returns:
- the hash code.
- See Also:
AbstractList.equals(java.lang.Object),List.hashCode()
-
equals
Description copied from class:AbstractListCompares 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.- Specified by:
equalsin interfaceCollection<E>- Specified by:
equalsin interfaceList<E>- Overrides:
equalsin classAbstractList<E>- Parameters:
o- the object to compare to this object.- Returns:
trueif the specified object is equal to this list,falseotherwise.- See Also:
AbstractList.hashCode()
-