public class ArrayList extends AbstractList implements List, Cloneable, Serializable, RandomAccess
List
, backed by an array. All
optional operations adding, removing, and replacing are supported. The
elements can be any objects.modCount
Constructor and Description |
---|
ArrayList()
Constructs a new instance of
ArrayList with ten capacity. |
ArrayList(Collection collection)
Constructs a new instance of
ArrayList containing the elements of
the specified collection. |
ArrayList(int capacity)
Constructs a new instance of
ArrayList with the specified
capacity. |
Modifier and Type | Method and Description |
---|---|
void |
add(int location,
Object object)
Inserts the specified object into this
ArrayList at the specified
location. |
boolean |
add(Object object)
Adds the specified object at the end of this
ArrayList . |
boolean |
addAll(Collection collection)
Adds the objects in the specified collection to this
ArrayList . |
boolean |
addAll(int location,
Collection 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. |
Object |
clone()
Returns a new
ArrayList with the same elements, the same size and
the same capacity as this ArrayList . |
boolean |
contains(Object 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. |
Object |
get(int location)
Returns the element at the specified location in this list.
|
int |
indexOf(Object 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. |
int |
lastIndexOf(Object object)
Searches this list for the specified object and returns the index of the
last occurrence.
|
Object |
remove(int location)
Removes the object at the specified location from this list.
|
boolean |
remove(Object object)
Removes one instance of the specified object from this
Collection if one
is contained (optional). |
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.
|
Object |
set(int location,
Object 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 . |
Object[] |
toArray()
Returns a new array containing all elements contained in this
ArrayList . |
Object[] |
toArray(Object[] 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. |
equals, hashCode, iterator, listIterator, listIterator, subList
containsAll, removeAll, retainAll, toString
finalize, getClass, notify, notifyAll, wait, wait, wait
containsAll, equals, hashCode, iterator, listIterator, listIterator, removeAll, retainAll, subList
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 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.collection
- the collection of elements to add.public void add(int location, Object 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.add
in interface List
add
in class AbstractList
location
- the index at which to insert the object.object
- the object to add.IndexOutOfBoundsException
- when location < 0 || > size()
public boolean add(Object object)
ArrayList
.add
in interface Collection
add
in interface List
add
in class AbstractList
object
- the object to add.public boolean addAll(int location, Collection collection)
addAll
in interface List
addAll
in class AbstractList
location
- the index at which to insert.collection
- the collection of objects.true
if this ArrayList
is modified, false
otherwise.IndexOutOfBoundsException
- when location < 0 || > size()
public boolean addAll(Collection collection)
ArrayList
.addAll
in interface Collection
addAll
in interface List
addAll
in class AbstractCollection
collection
- the collection of objects.true
if this ArrayList
is modified, false
otherwise.public void clear()
ArrayList
, leaving it empty.clear
in interface Collection
clear
in interface List
clear
in class AbstractList
isEmpty()
,
size
public Object clone()
ArrayList
with the same elements, the same size and
the same capacity as this ArrayList
.public boolean contains(Object object)
ArrayList
for the specified object.contains
in interface Collection
contains
in interface List
contains
in class AbstractCollection
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 Object get(int location)
AbstractList
get
in interface List
get
in class AbstractList
location
- the index of the element to return.public int indexOf(Object object)
AbstractList
indexOf
in interface List
indexOf
in class AbstractList
object
- the object to search for.public boolean isEmpty()
AbstractCollection
Collection
contains no elements. This implementation
tests, whether size
returns 0.isEmpty
in interface Collection
isEmpty
in interface List
isEmpty
in class AbstractCollection
true
if this Collection
has no elements, false
otherwise.AbstractCollection.size()
public int lastIndexOf(Object object)
AbstractList
lastIndexOf
in interface List
lastIndexOf
in class AbstractList
object
- the object to search for.public Object remove(int location)
remove
in interface List
remove
in class AbstractList
location
- the index of the object to remove.IndexOutOfBoundsException
- when location < 0 || >= size()
public boolean remove(Object object)
AbstractCollection
Collection
if one
is contained (optional). This 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 interface Collection
remove
in interface List
remove
in class AbstractCollection
object
- the object to remove.true
if this Collection
is modified, false
otherwise.protected void removeRange(int start, int end)
removeRange
in class AbstractList
start
- the index at which to start removing.end
- the index one after the end of the range to remove.IndexOutOfBoundsException
- when start < 0, start > end
or end > size()
public Object set(int location, Object object)
ArrayList
with the specified object.set
in interface List
set
in class AbstractList
location
- the index at which to put the specified object.object
- the object to add.IndexOutOfBoundsException
- when location < 0 || >= size()
public int size()
ArrayList
.size
in interface Collection
size
in interface List
size
in class AbstractCollection
ArrayList
.public Object[] toArray()
ArrayList
.toArray
in interface Collection
toArray
in interface List
toArray
in class AbstractCollection
ArrayList
public Object[] toArray(Object[] 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 interface Collection
toArray
in interface List
toArray
in class AbstractCollection
contents
- the array.ArrayList
.ArrayStoreException
- when the type of an element in this ArrayList
cannot
be stored in the type of the specified array.public void trimToSize()
ArrayList
to be the same as the current
size.size