Class ArrayList<E>
- All Implemented Interfaces:
Iterable<E>, Collection<E>, List<E>, RandomAccess
ArrayList is an implementation of List, backed by an array. All
optional operations adding, removing, and replacing are supported. The
elements can be any objects.
Since
1.2
-
Field Summary
Fields inherited from class AbstractList
modCount -
Constructor Summary
ConstructorsConstructorDescriptionConstructs a new instance ofArrayListwith ten capacity.ArrayList(int capacity) Constructs a new instance ofArrayListwith the specified capacity.ArrayList(Collection<? extends E> collection) Constructs a new instance ofArrayListcontaining the elements of the specified collection. -
Method Summary
Modifier and TypeMethodDescriptionvoidInserts the specified object into thisArrayListat the specified location.booleanAdds the specified object at the end of thisArrayList.booleanaddAll(int location, 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.booleanSearches thisArrayListfor the specified object.voidensureCapacity(int minimumCapacity) Ensures that after this operation theArrayListcan hold the specified number of elements without further growing.get(int location) Returns the element at the specified location in this list.intSearches this list for the specified object and returns the index of the first occurrence.booleanisEmpty()Returns if thisCollectioncontains no elements.intlastIndexOf(Object object) Searches this list for the specified object and returns the index of the last occurrence.remove(int location) Removes the object at the specified location from this list.booleanRemoves one instance of the specified object from thisCollectionif one is contained (optional).protected voidremoveRange(int start, int end) Removes the objects in the specified range from the start to the end, but not including the end index.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.voidSets the capacity of thisArrayListto be the same as the current size.Methods inherited from class AbstractList
equals, hashCode, iterator, listIterator, listIterator, subListMethods inherited from class AbstractCollection
containsAll, removeAll, retainAll, toStringMethods inherited from interface List
containsAll, equals, hashCode, iterator, listIterator, listIterator, removeAll, retainAll, subList
-
Constructor Details
-
ArrayList
public ArrayList()Constructs a new instance ofArrayListwith ten capacity. -
ArrayList
public ArrayList(int capacity) Constructs a new instance of
ArrayListwith the specified capacity.Parameters
capacity: the initial capacity of thisArrayList.
-
ArrayList
Constructs a new instance of
ArrayListcontaining the elements of the specified collection. The initial size of theArrayListwill be 10% larger than the size of the specified collection.Parameters
collection: the collection of elements to add.
-
-
Method Details
-
add
Inserts the specified object into this
ArrayListat 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.Parameters
-
location: the index at which to insert the object. -
object: the object to add.
Throws
IndexOutOfBoundsException: whenlocation size()
-
-
add
Adds the specified object at the end of this
ArrayList.Parameters
object: the object to add.
Returns
always true
- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceList<E>- Overrides:
addin classAbstractList<E>- Returns:
trueif thisCollectionis modified,falseotherwise.Throws
-
UnsupportedOperationException: if adding to thisCollectionis not supported. -
ClassCastException: @throws ClassCastException if the class of the object is inappropriate for this collection. -
IllegalArgumentException: if the object cannot be added to thisCollection. -
NullPointerException: if null elements cannot be added to theCollection.
-
-
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.
Parameters
-
location: the index at which to insert. -
collection: the collection of objects.
Returns
-
-
addAll
Adds the objects in the specified collection to this
ArrayList.Parameters
collection: the collection of objects.
Returns
- Specified by:
addAllin interfaceCollection<E>- Specified by:
addAllin interfaceList<E>- Overrides:
addAllin classAbstractCollection<E>- Returns:
trueif thisArrayListis modified,falseotherwise.
-
clear
public void clear()Removes all elements from this
ArrayList, leaving it empty.See also
-
#isEmpty
-
#size
- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceList<E>- Overrides:
clearin classAbstractList<E>
-
-
contains
Searches this
ArrayListfor the specified object.Parameters
object: the object to search for.
Returns
- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceList<E>- Overrides:
containsin classAbstractCollection<E>- Returns:
trueifobjectis an element of thisArrayList,falseotherwise
-
ensureCapacity
public void ensureCapacity(int minimumCapacity) Ensures that after this operation the
ArrayListcan 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.
Parameters
location: the index of the element to return.
Returns
the element at the specified index.
Throws
IndexOutOfBoundsException: iflocation = size()
-
indexOf
Description copied from class:AbstractListSearches this list for the specified object and returns the index of the first occurrence.
Parameters
object: the object to search for.
Returns
-
isEmpty
public boolean isEmpty()Description copied from class:AbstractCollectionReturns if this
Collectioncontains no elements. This implementation tests, whethersizereturns 0.Returns
- Specified by:
isEmptyin interfaceCollection<E>- Specified by:
isEmptyin interfaceList<E>- Overrides:
isEmptyin classAbstractCollection<E>- Returns:
trueif thisCollectionhas no elements,falseotherwise.See also
- #size
-
lastIndexOf
Description copied from class:AbstractListSearches this list for the specified object and returns the index of the last occurrence.
Parameters
object: the object to search for.
Returns
- Specified by:
lastIndexOfin interfaceList<E>- Overrides:
lastIndexOfin classAbstractList<E>- 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.
Parameters
location: the index of the object to remove.
Returns
the removed object.
Throws
IndexOutOfBoundsException: whenlocation = size()
-
remove
Description copied from class:AbstractCollectionRemoves one instance of the specified object from this
Collectionif 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.Parameters
object: the object to remove.
Returns
- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceList<E>- Overrides:
removein classAbstractCollection<E>- Returns:
trueif thisCollectionis modified,falseotherwise.Throws
-
UnsupportedOperationException: if removing from thisCollectionis not supported. -
ClassCastException: if the object passed is not of the correct type. -
NullPointerException: @throws NullPointerException ifobjectisnulland thisCollectiondoesn't supportnullelements.
-
-
removeRange
protected void removeRange(int start, int end) Removes the objects in the specified range from the start to the end, but not including the end index.
Parameters
-
start: the index at which to start removing. -
end: the index one after the end of the range to remove.
Throws
IndexOutOfBoundsException: whenstart endorend > size()
- Overrides:
removeRangein classAbstractList<E>
-
-
set
Replaces the element at the specified location in this
ArrayListwith the specified object.Parameters
-
location: the index at which to put the specified object. -
object: the object to add.
Returns
the previous element at the index.
Throws
IndexOutOfBoundsException: whenlocation = size()
-
-
size
public int size()Returns the number of elements in this
ArrayList.Returns
the number of elements in this
ArrayList.- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceList<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- how many objects this
Collectioncontains, orInteger.MAX_VALUEif there are more thanInteger.MAX_VALUEelements in thisCollection.
-
toArray
Returns a new array containing all elements contained in this
ArrayList.Returns
an array of the elements from this
ArrayList- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceList<E>- Overrides:
toArrayin classAbstractList<E>
-
toArray
public <T> 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 thisArrayList, the array element following the collection elements is set to null.Parameters
contents: the array.
Returns
an array of the elements from this
ArrayList.Throws
ArrayStoreException: @throws ArrayStoreException when the type of an element in thisArrayListcannot be stored in the type of the specified array.
- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceList<E>- Overrides:
toArrayin classAbstractList<E>
-
trimToSize
public void trimToSize()Sets the capacity of this
ArrayListto be the same as the current size.See also
- #size
-