E - The element type of this list.public class ArrayList<E> extends AbstractList<E> implements Cloneable, Serializable, RandomAccess
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.
modCount| Constructor and Description |
|---|
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. |
ArrayList(int capacity)
Constructs a new instance of
ArrayList with the specified
initial capacity. |
| Modifier and Type | Method and Description |
|---|---|
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). |
protected void |
removeRange(int fromIndex,
int toIndex)
Removes the objects in the specified range from the start to the end
index minus one.
|
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. |
Object[] |
toArray()
Returns a new array containing all elements contained in this
ArrayList. |
<T> T[] |
toArray(T[] contents)
Returns an array containing all elements contained in this
ArrayList. |
void |
trimToSize()
Sets the capacity of this
ArrayList to be the same as the current
size. |
listIterator, listIterator, subListcontainsAll, removeAll, retainAll, toStringfinalize, getClass, notify, notifyAll, wait, wait, waitcontainsAll, copyOf, of, of, of, of, of, of, of, of, of, of, of, of, removeAll, replaceAll, retainAll, sort, spliteratorforEach, parallelStream, removeIf, stream, toArraypublic ArrayList(int capacity)
ArrayList with the specified
initial capacity.capacity - the initial capacity of this ArrayList.public ArrayList()
ArrayList instance with zero initial capacity.public ArrayList(Collection<? extends E> collection)
ArrayList containing the elements of
the specified collection.collection - the collection of elements to add.public boolean add(E object)
ArrayList.add in interface Collection<E>add in interface List<E>add in class AbstractList<E>object - the object to add.public void add(int index,
E object)
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.add in interface List<E>add in class AbstractList<E>index - the index at which to insert the object.object - the object to add.IndexOutOfBoundsException - when location < 0 || location > size()public boolean addAll(Collection<? extends E> collection)
ArrayList.addAll in interface Collection<E>addAll in interface List<E>addAll in class AbstractCollection<E>collection - the collection of objects.true if this ArrayList is modified, false
otherwise.Collection.add(Object)public boolean addAll(int index,
Collection<? extends E> collection)
addAll in interface List<E>addAll in class AbstractList<E>index - the index at which to insert.collection - the collection of objects.true if this ArrayList is modified, false
otherwise.IndexOutOfBoundsException - when location < 0 || location > size()public void clear()
ArrayList, leaving it empty.public Object clone()
ArrayList with the same elements, the same size and
the same capacity as this ArrayList.public void ensureCapacity(int minimumCapacity)
ArrayList can hold the
specified number of elements without further growing.minimumCapacity - the minimum capacity asked for.public E get(int index)
AbstractListpublic int size()
ArrayList.size in interface Collection<E>size in interface List<E>size in class AbstractCollection<E>ArrayList.public boolean isEmpty()
AbstractCollectionCollection contains no elements. This implementation
tests, whether size returns 0.isEmpty in interface Collection<E>isEmpty in interface List<E>isEmpty in class AbstractCollection<E>true if this Collection has no elements, false
otherwise.AbstractCollection.size()public boolean contains(Object object)
ArrayList for the specified object.contains in interface Collection<E>contains in interface List<E>contains in class AbstractCollection<E>object - the object to search for.true if object is an element of this
ArrayList, false otherwisepublic int indexOf(Object object)
AbstractListpublic int lastIndexOf(Object object)
AbstractListlastIndexOf in interface List<E>lastIndexOf in class AbstractList<E>object - the object to search for.public E remove(int index)
remove in interface List<E>remove in class AbstractList<E>index - the index of the object to remove.IndexOutOfBoundsException - when location < 0 || location >= size()public boolean remove(Object object)
AbstractCollectionCollection 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.remove in interface Collection<E>remove in interface List<E>remove in class AbstractCollection<E>object - the object to remove.true if this Collection is modified, false
otherwise.protected void removeRange(int fromIndex,
int toIndex)
AbstractListremoveRange in class AbstractList<E>fromIndex - the index at which to start removing.toIndex - the index after the last element to remove.public E set(int index, E object)
ArrayList
with the specified object.set in interface List<E>set in class AbstractList<E>index - the index at which to put the specified object.object - the object to add.IndexOutOfBoundsException - when location < 0 || location >= size()public Object[] toArray()
ArrayList.toArray in interface Collection<E>toArray in interface List<E>toArray in class AbstractCollection<E>ArrayListArrays.asList(Object[])public <T> T[] toArray(T[] contents)
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.toArray in interface Collection<E>toArray in interface List<E>toArray in class AbstractCollection<E>T - the runtime type of the array to contain the collectioncontents - the array.ArrayList.ArrayStoreException - when the type of an element in this ArrayList cannot
be stored in the type of the specified array.public void trimToSize()
ArrayList to be the same as the current
size.sizepublic Iterator<E> iterator()
AbstractListpublic int hashCode()
AbstractListhashCode in interface Collection<E>hashCode in interface List<E>hashCode in class AbstractList<E>AbstractList.equals(java.lang.Object),
List.hashCode()public boolean equals(Object o)
AbstractListequals in interface Collection<E>equals in interface List<E>equals in class AbstractList<E>o - the object to compare to this object.true if the specified object is equal to this list,
false otherwise.AbstractList.hashCode()