Java Reference
In-Depth Information
Display 14.1
Some Methods in the Class
ArrayList
(part 3 of 4)
public boolean
remove(Object theElement)
Removes one occurrence of
theElement
from the calling
ArrayList
. If
theElement
is found in the
ArrayList
, then each element in the
ArrayList
with an index greater than the removed element'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
ArrayList
.
public void
clear()
Removes all elements from the calling
ArrayList
and sets the
ArrayList
's size to zero.
SEARCH METHODS
public boolean
contains(Object target)
Returns
true
if the calling
ArrayList
contains
target
; otherwise, returns
false
. Uses the method
equals
of the object
target
to test for equality with any element in the calling
ArrayList
.
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.
MEMORY MANAGEMENT (SIZE AND CAPACITY)
public boolean
isEmpty()
Returns
true
if the calling
ArrayList
is empty (that is, has size
0
); otherwise, returns
false
.
public int
size()
Returns the number of elements in the calling
ArrayList
.
public void
ensureCapacity(
int
newCapacity)
Increases the capacity of the calling
ArrayList
, if necessary, in order to ensure that the
ArrayList
can hold at least
newCapacity
elements. Using
ensureCapacity
can sometimes increase effi-
ciency, but its use is not needed for any other reason.
public void
trimToSize()
Trims the capacity of the calling
ArrayList
to the
ArrayList
's current size. This method is used to
save storage space.
(continued)