Java Reference
In-Depth Information
Display 16.7
Methods in the Classes
ArrayList<T>
and
Vector<T>
(part 4 of 4)
ITERATORS FOR BOTH
ArrayList<T>
AND
Vector<T>
public
Iterator<T> iterator()
Returns an iterator for the calling object. Iterators are discussed in Section 16.3.
public
ListIterator<T> listIterator()
Returns a
ListIterator<T>
for the calling object.
ListIterator<T>
is discussed in Section 16.3.
ListIterator<T> listIterator(
int
index)
Returns a list iterator for the calling object starting at
index
. The first element to be returned by
the iterator is the one at
index
. (Iterators are discussed in Section 16.3.)
CONVERTING TO AN ARRAY FOR BOTH
ArrayList<T>
AND
Vector<T>
public
Object[ ] toArray()
Returns an array containing all of the elements in the calling object. The elements of the array are
indexed the same as in the calling object.
public
<E> E[ ] toArray(E[ ] a)
Note that the type parameter
E
is not the same as
T
. So,
E
can be any reference type; it need not
be the type
T
in
Collection<T>
. For example,
E
might be an ancestor type of
T
.
Returns an array containing all of the elements in the calling object. The elements of the array are
indexed the same as in the calling object.
The argument
a
is used primarily to specify the type of the array returned. The exact details are
described next.
The type of the returned array is that of
a
. If the collection fits in the array
a
, then
a
is used to hold
the elements of the returned array; otherwise a new array is created with the same type as
a
.
If
a
has more elements than the calling object, then the element in
a
immediately following the
end of the elements copied from the calling object is set to
null
.
MEMORY MANAGEMENT FOR BOTH
ArrayList<T>
AND
Vector<T>
public int
size()
Returns the number of elements in the calling object.
public int
capacity()
Returns the current capacity of the calling object.
public void
ensureCapacity(
int
newCapacity)
Increases the capacity of the calling object to ensure that it can hold at least
newCapacity
elements. Using
ensureCapacity
can sometimes increase efficiency, but it is not needed for
any other reason.
public void
trimToSize()
Trims the capacity of the calling object to be the calling object's current size. This is used to save storage.
MAKE A COPY FOR BOTH
ArrayList<T>
AND
Vector<T>
public
Object clone()
Returns a shallow copy of the calling object.