Class ArrayList<E>
java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
java.util.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 ofArrayListwith the specified capacity.- Parameters:
capacity- the initial capacity of thisArrayList.
-
ArrayList
Constructs a new instance ofArrayListcontaining 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 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:
location- the index at which to insert the object.object- the object to add.- Throws:
IndexOutOfBoundsException- whenlocation < 0 || > size()
-
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
-
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:
location- the index at which to insert.collection- the collection of objects.- Returns:
trueif thisArrayListis modified,falseotherwise.- Throws:
IndexOutOfBoundsException- whenlocation < 0 || > 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.
-
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:
-
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
-
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. -
indexOf
Description copied from class:AbstractListSearches this list for the specified object and returns the index of the first occurrence. -
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:
-
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:
location- the index of the object to remove.- Returns:
- the removed object.
- Throws:
IndexOutOfBoundsException- whenlocation < 0 || >= 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 start, int end) Removes the objects in the specified range from the start to the end, but not including the end index.- Overrides:
removeRangein classAbstractList<E>- Parameters:
start- the index at which to start removing.end- the index one after the end of the range to remove.- Throws:
IndexOutOfBoundsException- whenstart < 0, start > endorend > size()
-
set
Replaces the element at the specified location in thisArrayListwith the specified object.- Specified by:
setin interfaceList<E>- Overrides:
setin classAbstractList<E>- 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 < 0 || >= size()
-
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.
-
toArray
Returns a new array containing all elements contained in thisArrayList.- Specified by:
toArrayin interfaceCollection<E>- Specified by:
toArrayin interfaceList<E>- Overrides:
toArrayin classAbstractList<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 classAbstractList<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:
-