Java Reference
In-Depth Information
CONSTRUCTORS
public
ArrayList(
int
initialCapacity)
Creates an empty
ArrayList<T>
with the specifi ed initial capacity. When the
ArrayList<T>
needs to increase its capacity, the capacity doubles.
Throws an
IllegalArgumentException
if
initialCapacity
is negative.
public
ArrayList()
Creates an empty
ArrayList<T>
with an initial capacity of
10
. When the
ArrayList<T>
needs to increase its capacity, the capacity doubles.
public
ArrayList(Collection<?
extends
<T> c)
Creates an
ArrayList<T>
that contains all the elements of the collection
c
in the same order
as they have in
c
. In other words, the elements have the same index in the
ArrayList<T>
created as they do in
c
. This is not quite a true copy constructor because it does not preserve
capacity. The capacity of the created
ArrayList<T>
will be
c.size()
, not
c.capacity
.
The
ArrayList<T>
created is only a shallow copy of the collection argument. The
ArrayList<T>
created contains references to the elements in
c
(not references to clones of
the elements in
c
).
Throws:
NullPointerException
if
c
is
null.
ARRAYLIKE METHODS
public
T set(
int
index, T newElement)
Sets the element at the specifi ed
index
to
newElement
. The element previously at that position
is returned. If you draw an analogy to an array
a
, this is analogous to setting
a [index]
to the
value
newElement.
The
index
must be a value greater than or equal to
0
and strictly less than
the current size of the list.
Throws:
IndexOutOfBoundsException
if the
index
is not in this range.
public
T get(
int
index)
Returns the element at the specifi ed index. This is analogous to returning
a[index]
for an
array
a
. The
index
must be a value greater than or equal to
0
and less than the current size of
the calling object.
Throws:
IndexOutOfBoundsException
if the
index
is not in the required range.