Java Reference
In-Depth Information
Display 16.7
Methods in the Classes
ArrayList<T>
and
Vector<T>
(part 2 of 4)
public
Vector(int initialCapacity,
int
capacityIncrement)
Constructs an empty vector with the specified initial capacity and capacity increment. When the
vector needs to grow, it will add room for
capacityIncrement
more items.
(
ArrayList<T>
does not have a corresponding constructor.)
ARRAYLIKE METHODS FOR BOTH
ArrayList<T>
AND
Vector<T>
public
T set(
int
index, T newElement)
Sets the element at the specified
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 zero and strictly less than
the current size of the list.
public
T get(
int
index)
Returns the element at the specified 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.
METHODS TO ADD ELEMENTS FOR BOTH
ArrayList<T>
AND
Vector<T>
public
boolean
add(T newElement)
Adds
newElement
to the end of the calling object's list and increases its size by one. The capacity
of the calling object is increased if that is required. Returns
true
if the add was successful. This
method is often used as if it were a
void
method.
public
void
add(
int
index, T newElement)
Inserts
newElement
as an element in the calling object at the specified index and increases the size
of the calling object by one. Each element in the calling object with an index greater than or equal to
index is shifted upward to have an index that is one greater than the value it had previously.
The index must be a value greater than or equal to zero and less than or equal to the size of the
calling object (before this addition).
Note that you can use this method to add an element after the last current element. The capacity
of the calling object is increased if that is required.
public
boolean
addAll(Collection<?
extends
T> c)
Appends all the elements in
c
to the end of the elements in the calling object in the order that
they are enumerated by a
c
iterator. The behavior of this method is not guaranteed if the collec-
tion
c
is the calling object or any collection including the calling object either directly or indirectly.
public
boolean
addAll(
int
index, Collection<?
extends
T> c)
Inserts all the elements in
c
into the calling object starting at position
index
. Elements are
inserted in the order that they are enumerated by a
c
iterator. Elements previously at positions
index
or higher are shifted to higher numbered positions.