Java Reference
In-Depth Information
Display 16.7
Methods in the Classes
ArrayList<T>
and
Vector<T>
(part 3 of 4)
METHODS TO REMOVE ELEMENTS FOR BOTH
ArrayList<T>
AND
Vector<T>
public
T remove(
int
index)
Deletes the element at the specified index and returns the element deleted. The size of the call-
ing object is decreased by one. The capacity of the calling object is not changed. Each element in
the calling object with an index greater than or equal to
index
is decreased to have an index that
is one less than the value it had previously.
The
index
must be a value greater than or equal to zero and less than the size of the calling
object (before this removal).
public
boolean
remove(Object theElement)
Removes the first occurrence of
theElement
from the calling object. If
theElement
is found in the call-
ing object, then each element in the calling object with an index greater than or equal to
theElement
's
index is decreased to have an index that is one less than the value it had previously. Returns
true
if
theElement
was found (and removed). Returns
false
if
theElement
was not found in the calling
object. If the element was removed, the size is decreased by one. The capacity is not changed.
protected
void
removeRange(
int
fromIndex,
int
toIndex)
Removes all elements with index greater than or equal to
fromIndex
and strictly less than
toIndex
. Be sure to note that this method is
protected
, not
public
.
public
void
clear( )
Removes all elements from the calling object and sets its size to zero.
SEARCH METHODS FOR BOTH
ArrayList<T>
AND
Vector<T>
public
boolean
isEmpty( )
Returns
true
if the calling object is empty (that is, has size
0
); otherwise returns
false
.
public
boolean
contains(Object target)
Returns
true
if
target
is an element of the calling object; otherwise returns
false
. Uses the
method
equals
of the object
target
to test for equality.
public
int
indexOf(Object target)
Returns the index of the first element that is equal to
target
. Uses the method
equals
of the
object
target
to test for equality. Returns
1
if
target
is not found.
public
int
lastIndexOf(Object target)
Returns the index of the last element that is
equal
to
target
. Uses the method
equals
of the
object
target
to test for equality. Returns
1
if
target
is not found.
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.
(continued)