public final class ArrayList<E> extends List<E>
List
, backed by an array. All optional operations adding, removing, and
replacing are supported. The elements can be any objects.
Changes from the java.util version:
Does not support clone; use constructor taking a Collection argument instead (which is more flexible and type safe)Modifier and Type | Field and Description |
---|---|
protected int |
modCount
A counter for changes to the list.
|
Constructor and Description |
---|
ArrayList()
Constructs a new instance of
ArrayList with ten capacity. |
ArrayList(Collection<E> collection)
Constructs a new instance of
ArrayList containing the elements of the specified collection. |
ArrayList(E... args)
Constructs an ArrayList, with the specified initial members.
|
ArrayList(int capacity)
Constructs a new instance of
ArrayList with the specified capacity. |
Modifier and Type | Method and Description |
---|---|
boolean |
add(E object)
Adds the specified object at the end of this
ArrayList . |
void |
add(int location,
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 location,
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. |
boolean |
contains(E 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. |
E |
get(int location)
Returns the element at the specified location in this list.
|
int |
indexOf(E 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(E object)
Searches this
List for the specified object and returns the index of the last occurrence. |
boolean |
remove(E object)
Removes one instance of the specified object from this
Collection if one is contained (optional). |
E |
remove(int location)
Removes the object at the specified location from this list.
|
E |
set(int location,
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 . |
void |
sortInPlace(Comparator<E> comparator) |
java.lang.Object[] |
toArray()
Returns a new array containing all elements contained in this
ArrayList . |
void |
toArray(E[] 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. |
containsAll, removeAll, retainAll, toString
public ArrayList()
ArrayList
with ten capacity.public ArrayList(int capacity)
ArrayList
with the specified capacity.capacity
- the initial capacity of this ArrayList
.public ArrayList(Collection<E> collection)
ArrayList
containing the elements of the specified collection. The initial
size of the ArrayList
will be 10% larger than the size of the specified collection.
Changes from the java.util version: The constructor there supported covariance, taking a Collection<? extends
E> argument where the collection can be of a subclass of E. This constructor only allows collections of
exact type E, but you can use the addAll method instead if you really need to copy a collection of a subclass.collection
- the collection of elements to addpublic ArrayList(E... args)
args
- elements to add initially to the listpublic void add(int location, 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.public boolean add(E object)
ArrayList
.public boolean addAll(int location, Collection<? extends E> collection)
public boolean addAll(Collection<? extends E> collection)
ArrayList
.addAll
in class Collection<E>
collection
- the collection of objects.true
if this ArrayList
is modified, false
otherwise.public void clear()
ArrayList
, leaving it empty.clear
in class Collection<E>
isEmpty()
,
size()
public boolean contains(E object)
ArrayList
for the specified object.contains
in class Collection<E>
object
- the object to search for.true
if object
is an element of this ArrayList
, false
otherwisepublic 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 location)
List
public int indexOf(E object)
List
List
for the specified object and returns the index of the first occurrence.public boolean isEmpty()
Collection
Collection
contains no elements.
The default implementation tests whether size
returns 0.isEmpty
in class Collection<E>
true
if this Collection
has no elements, false
otherwise.Collection.size()
public int lastIndexOf(E object)
List
List
for the specified object and returns the index of the last occurrence.lastIndexOf
in class List<E>
object
- the object to search for.public Iterator<E> iterator()
iterator
in interface java.lang.Iterable<E>
iterator
in class Collection<E>
Iterator
public E remove(int location)
public boolean remove(E object)
Collection
Collection
if one is contained (optional). The
element elem
that is removed complies with (object==null ? elem==null : object.equals(elem)
.
The default 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 class Collection<E>
object
- the object to remove.true
if this Collection
is modified, false
otherwise.public E set(int location, E object)
ArrayList
with the specified object.public int size()
ArrayList
.size
in class Collection<E>
ArrayList
.public java.lang.Object[] toArray()
ArrayList
.toArray
in class Collection<E>
ArrayList
public void toArray(E[] 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 class Collection<E>
contents
- the array.java.lang.ArrayStoreException
- when the type of an element in this ArrayList
cannot be stored in the type of
the specified array.public void sortInPlace(Comparator<E> comparator)
public void trimToSize()
ArrayList
to be the same as the current size.size()