public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable
Each list has a capacity, and as the array reaches that capacity it is automatically transferred to a larger array. You also have access to ensureCapacity and trimToSize to control the backing array's size, avoiding reallocation or wasted memory.
ArrayList is not synchronized, so if you need multi-threaded access,
consider using:
List l = Collections.synchronizedList(new ArrayList(...));
The iterators are fail-fast, meaning that any structural
modification, except for remove()
called on the iterator
itself, cause the iterator to throw a
ConcurrentModificationException
rather than exhibit
non-deterministic behavior.
Collection
,
List
,
LinkedList
,
Vector
,
Collections.synchronizedList(List)
,
AbstractList
Constructor and Description |
---|
ArrayList()
Construct a new ArrayList with the default capacity (16).
|
ArrayList(Collection<? extends E> c)
Construct a new ArrayList, and initialize it with the elements
in the supplied Collection.
|
ArrayList(int capacity)
Construct a new ArrayList with the supplied initial capacity.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E e)
Appends the supplied element to the end of this list.
|
void |
add(int index,
E e)
Adds the supplied element at the specified index, shifting all
elements currently at that index or higher one to the right.
|
boolean |
addAll(Collection<? extends E> c)
Add each element in the supplied Collection to this List.
|
boolean |
addAll(int index,
Collection<? extends E> c)
Add all elements in the supplied collection, inserting them beginning
at the specified index.
|
void |
clear()
Removes all elements from this List
|
Object |
clone()
Creates a shallow copy of this ArrayList (elements are not cloned).
|
boolean |
contains(Object e)
Returns true iff element is in this ArrayList.
|
void |
ensureCapacity(int minCapacity)
Guarantees that this list will have at least enough capacity to
hold minCapacity elements.
|
E |
get(int index)
Retrieves the element at the user-supplied index.
|
int |
indexOf(Object e)
Returns the lowest index at which element appears in this List, or
-1 if it does not appear.
|
boolean |
isEmpty()
Checks if the list is empty.
|
int |
lastIndexOf(Object e)
Returns the highest index at which element appears in this List, or
-1 if it does not appear.
|
E |
remove(int index)
Removes the element at the user-supplied index.
|
E |
set(int index,
E e)
Sets the element at the specified index.
|
int |
size()
Returns the number of elements in this list.
|
Object[] |
toArray()
Returns an Object array containing all of the elements in this ArrayList.
|
<T> T[] |
toArray(T[] a)
Returns an Array whose component type is the runtime component type of
the passed-in Array.
|
void |
trimToSize()
Trims the capacity of this List to be equal to its size;
a memory saver.
|
equals, hashCode, iterator, listIterator, listIterator, subList
containsAll, remove, removeAll, retainAll, toString
containsAll, equals, hashCode, iterator, listIterator, listIterator, remove, removeAll, retainAll, subList
public ArrayList()
public ArrayList(Collection<? extends E> c)
c
- the collection whose elements will initialize this listNullPointerException
- if c is nullpublic ArrayList(int capacity)
capacity
- initial capacity of this ArrayListIllegalArgumentException
- if capacity is negativepublic boolean add(E e)
add
in interface Collection<E>
add
in interface List<E>
add
in class AbstractList<E>
e
- the element to be appended to this listAbstractList.add(int, Object)
public void add(int index, E e)
add
in interface List<E>
add
in class AbstractList<E>
index
- the index at which the element is being addede
- the item being addedIndexOutOfBoundsException
- if index < 0 || index > size()AbstractList.modCount
public boolean addAll(Collection<? extends E> c)
addAll
in interface Collection<E>
addAll
in interface List<E>
addAll
in class AbstractCollection<E>
c
- a Collection containing elements to be added to this ListNullPointerException
- if c is nullAbstractCollection.add(Object)
public boolean addAll(int index, Collection<? extends E> c)
addAll
in interface List<E>
addAll
in class AbstractList<E>
index
- the index at which the elements will be insertedc
- the Collection containing the elements to be insertedIndexOutOfBoundsException
- if index < 0 || index > 0NullPointerException
- if c is nullAbstractList.add(int, Object)
public void clear()
clear
in interface Collection<E>
clear
in interface List<E>
clear
in class AbstractList<E>
AbstractList.remove(int)
,
AbstractList.removeRange(int, int)
public Object clone()
public boolean contains(Object e)
contains
in interface Collection<E>
contains
in interface List<E>
contains
in class AbstractCollection<E>
e
- the element whose inclusion in the List is being testedpublic void ensureCapacity(int minCapacity)
minCapacity
- the minimum guaranteed capacitypublic E get(int index)
get
in interface List<E>
get
in class AbstractList<E>
index
- the index of the element we are fetchingIndexOutOfBoundsException
- if index < 0 || index >= size()public int indexOf(Object e)
public boolean isEmpty()
isEmpty
in interface Collection<E>
isEmpty
in interface List<E>
isEmpty
in class AbstractCollection<E>
AbstractCollection.size()
public int lastIndexOf(Object e)
lastIndexOf
in interface List<E>
lastIndexOf
in class AbstractList<E>
e
- the element whose inclusion in the List is being testedpublic E remove(int index)
remove
in interface List<E>
remove
in class AbstractList<E>
index
- the index of the element to be removedIndexOutOfBoundsException
- if index < 0 || index >= size()AbstractList.modCount
public E set(int index, E e)
set
in interface List<E>
set
in class AbstractList<E>
index
- the index at which the element is being sete
- the element to be setIndexOutOfBoundsException
- if index < 0 || index >= 0public int size()
size
in interface Collection<E>
size
in interface List<E>
size
in class AbstractCollection<E>
public Object[] toArray()
toArray
in interface Collection<E>
toArray
in interface List<E>
toArray
in class AbstractCollection<E>
public <T> T[] toArray(T[] a)
toArray
in interface Collection<E>
toArray
in interface List<E>
toArray
in class AbstractCollection<E>
a
- the passed-in ArrayArrayStoreException
- if the runtime type of a does not allow
an element in this listNullPointerException
- if a is nullpublic void trimToSize()