public class ArrayList
extends java.util.AbstractList
implements java.util.Collection
| Constructor and Description |
|---|
ArrayList()
Constructs an empty ArrayList so that its internal data array has size
10 and its standard capacity increment is zero.
|
ArrayList(java.util.Collection c)
Constructs an ArrayList containing the objects in the specified collection,
in the order they are returned by the collection's iterator.
|
ArrayList(int initialCapacity)
Constructs an empty ArrayList with the specified initial capacity and
with its capacity increment equal to zero.
|
ArrayList(int initialCapacity,
int capacityIncrement)
Constructs an empty ArrayList with the specified initial capacity and
capacity increment.
|
ArrayList(java.lang.Object[] a)
Constructs an ArrayList containing the objects contained in the
specified array, in the same order.
|
| Modifier and Type | Method and Description |
|---|---|
void |
add(int index,
java.lang.Object element)
Inserts the specified element at the specified position in this
ArrayList.
|
boolean |
add(java.lang.Object element)
Appends the specified element to the end of this ArrayList, growing
the array if necessary.
|
boolean |
addAll(ArrayList v)
Appends all of the elements in the specified ArrayList to the end
of this ArrayList.
|
void |
addAll(int index,
ArrayList v)
Inserts all of the elements in the specified ArrayList into this
ArrayList at the specified position.
|
void |
addAll(int index,
java.lang.Object[] a)
Inserts all the elements in the specified array at the specified
index in this ArrayList.
|
boolean |
addAll(java.lang.Object[] a)
Appends all of the elements in the specified array to the end
of this ArrayList, in the order of the array.
|
void |
addElement(java.lang.Object element)
Adds the specified component to the end of this ArrayList, increasing
its size by one.
|
int |
capacity()
Returns the current capacity of this ArrayList.
|
void |
clear()
Removes all of the elements from this ArrayList.
|
void |
clear(int index)
Clears all elements of this arraylist at index equal to or greater
than the specified index.
|
java.lang.Object |
clone()
Returns a clone of this ArrayList.
|
boolean |
contains(java.lang.Object element)
Tests if the specified Object is a component in this ArrayList.
|
boolean |
containsAll(ArrayList v)
Returns true if this ArrayList contains all of the elements in the
specified ArrayList.
|
boolean |
containsAll(java.lang.Object[] a)
Returns true if this ArrayList contains all of the elements in the
specified array.
|
void |
copyInto(java.lang.Object[] a)
Copies the components of this ArrayList into the specified array
a.
|
java.lang.Object |
elementAt(int index)
Returns the component at the specified index.
|
void |
ensureCapacity(int minCapacity)
Increases the capacity of this ArrayList, if necessary, to ensure that
it can hold at least the number of components specified by the
minimum capacity argument.
|
boolean |
equals(java.lang.Object o)
Compares the specified object with this ArrayList for equality.
|
java.lang.Object |
firstElement()
Returns the first component (the item at index 0) of this ArrayList.
|
java.lang.Object |
get(int index)
Returns the element at the specified position in this ArrayList.
|
boolean |
hasGaps()
Returns true iff there are null elements between
0 and size()-1.
|
int |
hashCode()
Returns the hash code value for this ArrayList.
|
int |
indexOf(java.lang.Object element)
Searches for the first occurrence of the given Object; returns -1 if it
is not found.
|
int |
indexOf(java.lang.Object element,
int index)
Searches for the first occurence of the given Object, beginning
the search at index; returns -1 if it is not found.
|
void |
insertElementAt(java.lang.Object element,
int index)
Inserts the specified Object as a component in this ArrayList at the
specified index.
|
boolean |
isEmpty()
Tests if this ArrayList has no components.
|
java.util.Iterator |
iterator() |
java.lang.Object |
lastElement()
Returns the last component of the ArrayList.
|
int |
lastIndexOf(java.lang.Object element)
Returns the index of the last occurrence of the specified Object in
this ArrayList; returns -1 if it is not found.
|
int |
lastIndexOf(java.lang.Object element,
int index)
Searches backwards for the specified Object, starting from the
specified index, and returns its index, or -1 if not found.
|
java.lang.Object |
remove(int index)
Removes the element at the specified position and shifts any subsequent
elements to the left (subtracts one from their indices).
|
boolean |
remove(java.lang.Object element)
Removes the first occurrence of the specified element.
|
boolean |
removeAll(ArrayList v)
Removes from this ArrayList all of its elements that are contained
in the specified ArrayList.
|
boolean |
removeAll(java.lang.Object[] a)
Removes from this ArrayList all of its elements that are contained
in the specified array.
|
void |
removeAllElements()
Removes all components from this ArrayList.
|
boolean |
removeAllFirst(ArrayList v)
Removes from this ArrayList the first occurrence of the elements
that are contained in the specified ArrayList.
|
boolean |
removeAllFirst(java.lang.Object[] a)
Removes from this ArrayList the first occurrence of the elements
that are contained in the specified array
|
boolean |
removeElement(java.lang.Object element)
Removes the first occurrence of the specified element.
|
void |
removeElementAt(int index)
Deletes the component at the specified index.
|
void |
removeGaps()
Eliminates null gaps from this ArrayList, compressing it
down so that all elements between 0 and size()-1
are non-null.
|
void |
removeRange(int fromIndex,
int toIndex)
Removes from this ArrayList all of the elements whose index is
between fromIndex, inclusive and toIndex, exclusive.
|
boolean |
retainAll(ArrayList v)
Retains only the elements in this ArrayList whose elements are
contained in the specified ArrayList.
|
java.lang.Object |
set(int index,
java.lang.Object element)
Replaces the element at the specified position in this ArrayList
with the specified element.
|
void |
setElementAt(java.lang.Object element,
int index)
Sets the component at the specified index of this ArrayList to be the
specified Object.
|
void |
setIncrementFactor(float incrementFactor) |
void |
setLast(java.lang.Object element)
Replaces the element at the last position in this ArrayList with
the specified element.
|
void |
setSize(int newSize)
Sets the size of this ArrayList.
|
int |
size()
Returns the number of components in this ArrayList.
|
java.lang.Object[] |
toArray()
Returns an array containing all of the elements in this ArrayList in
the correct order.
|
java.lang.String |
toString()
Returns a string representation of this ArrayList
|
java.lang.String[] |
toStringArray()
Returns an array containing all of the string forms of the elements
in this ArrayList in the correct order.
|
void |
trimToSize()
Trims the capacity of this ArrayList to be the ArrayList's current
size.
|
addAll, containsAll, removeAll, retainAll, toArraypublic ArrayList(int initialCapacity,
int capacityIncrement)
initialCapacity - the initial capacity of the ArrayList.capacityIncrement - amount by which the capacity is increased on overflow.java.lang.NegativeArraySizeException - if the specified initial capacity is negative.public ArrayList(int initialCapacity)
initialCapacity - the initial capacity of the ArrayList.java.lang.NegativeArraySizeException - if the specified initial capacity is negative.public ArrayList()
public ArrayList(java.lang.Object[] a)
a - an array of Objects.public ArrayList(java.util.Collection c)
c - the collection whose elements are to be placed into this ArrayList.public final void copyInto(java.lang.Object[] a)
a - the array into which the components get copied.java.lang.IndexOutOfBoundsException - if the array is too small.public final void trimToSize()
public final void ensureCapacity(int minCapacity)
If the current capacity of this ArrayList is less than minCapacity, then its capacity is increased by replacing its internal data array, kept in the field _elementData, with a larger one. The size of the new data array will be the old size plus capacityIncrement, unless the value of capacityIncrement is less than or equal to zero, in which case the new capacity will be the old capacity multiplied by incrementFactor; but if this new size is still smaller than minCapacity, then the new capacity will be minCapacity.
minCapacity - the desired minimum capacity.public final void setSize(int newSize)
newSize - the new size of this ArrayList.java.lang.ArrayIndexOutOfBoundsException - if new size is negative.public final int capacity()
public final int size()
size in interface java.util.Collectionsize in interface java.util.Listsize in class java.util.AbstractCollectionpublic final boolean hasGaps()
public final void removeGaps()
public final boolean isEmpty()
isEmpty in interface java.util.CollectionisEmpty in interface java.util.ListisEmpty in class java.util.AbstractCollectionpublic final boolean contains(java.lang.Object element)
contains in interface java.util.Collectioncontains in interface java.util.Listcontains in class java.util.AbstractCollectionelement - a Object.public final int indexOf(java.lang.Object element)
indexOf in interface java.util.ListindexOf in class java.util.AbstractListelement - a Object.public final int indexOf(java.lang.Object element,
int index)
element - a Object.index - the non-negative index to start searching from.java.lang.IndexOutOfBoundsException - if index is negative.public final int lastIndexOf(java.lang.Object element)
lastIndexOf in interface java.util.ListlastIndexOf in class java.util.AbstractListelement - the desired component.public final int lastIndexOf(java.lang.Object element,
int index)
element - a Object.index - the non-negative index to start searching from.java.lang.IndexOutOfBoundsException - if index is invalid.public final java.lang.Object elementAt(int index)
index - an index into this ArrayList.java.lang.ArrayIndexOutOfBoundsException - if the index is invalid.public final java.lang.Object firstElement()
java.lang.ArrayIndexOutOfBoundsException - if this ArrayList is empty.public final java.lang.Object lastElement()
java.lang.ArrayIndexOutOfBoundsException - if this ArrayList is empty.public final void setElementAt(java.lang.Object element,
int index)
The index must be a value greater than or equal to 0 and less than the current size of the ArrayList.
This method is identical in functionality to the set method except that the latter returns the old object that was stored at the specified position and it has its argument in a different order.
element - what the component is to be set to.index - the specified index.java.lang.ArrayIndexOutOfBoundsException - if the index was invalid.public final void removeElementAt(int index)
The index must be a value greater than or equal to 0 and less than the current size of the ArrayList.
This method is identical in functionality to the remove(int) method except that the remove(int) method returns the old value that was stored at the specified position.
index - the index of the Object to remove.java.lang.ArrayIndexOutOfBoundsException - if the index was invalid.public final void insertElementAt(java.lang.Object element,
int index)
The index must be a value greater than or equal to 0 and less than or equal to the current size of the ArrayList. (If the index is equal to the current size of the ArrayList, the new element is appended to the ArrayList.)
This method is identical in functionality to the add(int, Object) method except that the latter method reverses the order of the arguments, to match array usage more closely.
element - the element to insert.index - where to insert the new component.java.lang.ArrayIndexOutOfBoundsException - if the index was invalid.public final void addElement(java.lang.Object element)
This method is identical in functionality to the add(Object) method except that the latter returns a boolean.
element - the component to be added.public final void removeAllElements()
public final java.lang.Object clone()
clone in class java.lang.Objectpublic final java.lang.Object[] toArray()
toArray in interface java.util.CollectiontoArray in interface java.util.ListtoArray in class java.util.AbstractCollectionpublic final java.lang.String[] toStringArray()
public final java.lang.Object get(int index)
get in interface java.util.Listget in class java.util.AbstractListjava.lang.ArrayIndexOutOfBoundsException - if the index is invalid.public final java.lang.Object set(int index,
java.lang.Object element)
set in interface java.util.Listset in class java.util.AbstractListindex - index of element to replace.element - element to be stored at the specified position.java.lang.ArrayIndexOutOfBoundsException - if index is out of range.public final void setLast(java.lang.Object element)
element - element to be stored at the last position.java.lang.ArrayIndexOutOfBoundsException - if index is out of range.public final boolean add(java.lang.Object element)
add in interface java.util.Collectionadd in interface java.util.Listadd in class java.util.AbstractListelement - object to be appended to this ArrayList.public final boolean remove(java.lang.Object element)
N.B.Because remove(Object) is ambiguous, it is not supported for ArrayLists; its functionality is provided by removeIndex(int) and removeElement(Object).
remove in interface java.util.Collectionremove in interface java.util.Listremove in class java.util.AbstractCollectionelement - object to be removed from this ArrayList, if present.public final boolean removeElement(java.lang.Object element)
element - element to be removed from this ArrayList, if present.public final void add(int index,
java.lang.Object element)
add in interface java.util.Listadd in class java.util.AbstractListindex - index at which the specified element is to be inserted.element - object to be inserted.java.lang.ArrayIndexOutOfBoundsException - if index is out of range.public final java.lang.Object remove(int index)
remove in interface java.util.Listremove in class java.util.AbstractListindex - the index of the element to removed.java.lang.ArrayIndexOutOfBoundsException - if index is out of range.public final void clear()
clear in interface java.util.Collectionclear in interface java.util.Listclear in class java.util.AbstractListpublic final boolean containsAll(java.lang.Object[] a)
a - array containing the elements to be tested for membership.public final boolean containsAll(ArrayList v)
v - ArrayList of elements to be tested for membership.public final boolean addAll(java.lang.Object[] a)
a - array containing the elements to be inserted.java.lang.ClassCastException - if the collection contains a non-number.public final boolean addAll(ArrayList v)
v - the ArrayList of elements to be added.public final boolean removeAll(java.lang.Object[] a)
a - array of elements to be removed.public final boolean removeAll(ArrayList v)
v - the ArrayList of elements to be removed.public final boolean removeAllFirst(java.lang.Object[] a)
a - array of elements to be removed.public final boolean removeAllFirst(ArrayList v)
v - the ArrayList of elements to be removed.public final boolean retainAll(ArrayList v)
v - the ArrayList of elements to be retained.public final void addAll(int index,
java.lang.Object[] a)
index - index at which the specified element is to be inserted.a - arrays of element to be inserted.java.lang.ArrayIndexOutOfBoundsException - if index is out of range.public final void addAll(int index,
ArrayList v)
index - index where to insert the first elementv - ArrayList to be inserted into this ArrayList.java.lang.ArrayIndexOutOfBoundsException - if index is invalid.public final boolean equals(java.lang.Object o)
equals in interface java.util.Collectionequals in interface java.util.Listequals in class java.util.AbstractListo - the object to be compared for equality with this ArrayList.public int hashCode()
hashCode in interface java.util.CollectionhashCode in interface java.util.ListhashCode in class java.util.AbstractListpublic java.lang.String toString()
toString in class java.util.AbstractCollectionpublic void removeRange(int fromIndex,
int toIndex)
N.B. Note that this method is public, whereas - strangely enough, it is protected in the java.util.ArrayList API!
removeRange in class java.util.AbstractListfromIndex - index of first element to be removed.toIndex - index after last element to be removed.public final void setIncrementFactor(float incrementFactor)
incrementFactor - a float that is between 1.1 and 5.0java.lang.IllegalArgumentException - if not between 1.1 and 5.0public final void clear(int index)
index - the index above which clearing takes effectpublic java.util.Iterator iterator()
iterator in interface java.lang.Iterableiterator in interface java.util.Collectioniterator in interface java.util.Listiterator in class java.util.AbstractList