Java Reference
In-Depth Information
You can remove the reference at a particular index position by calling the
remove()
method with the index
position of the object as the argument. For example:
names.remove(3);
This removes the fourth element from the
names
vector. The elements that follow the one that was re-
moved are now at index positions that are one less than they were before, so what was previously the fifth
element is now at index position 3. Of course, the index value that you specify must be legal for the vector,
so it must be greater than or equal to 0 and less than the size of the vector, or an
ArrayIndexOutOfBound-
sException
is thrown. The
remove()
method returns a reference to the object removed, so you can retain a
reference to the object after you remove it:
String name = names.remove(3);
Here you save the reference to the object that was removed in
name
.
Sometimes, you want to remove a particular reference, rather than the reference at a given index. If you
know what the object is that you want to remove, you can use another version of the
remove()
method to
delete it:
boolean deleted = names.remove(aName);
This searches the
names
vector from the beginning to find the first reference to the object
aName
and re-
moves it. If the object is found and removed from the vector, the method returns
true
; otherwise, it returns
false
. When you are not sure that the element to be removed is present in the vector, you can test the value
returned by the
remove()
method.
Another way to remove a single element is to use the
removeElementAt()
method, which requires an
argument specifying the index position of the element to be removed. This is similar to the version of
re-
move()
that accepts an index as an argument, the difference being that here the return type is
void
. This is
because the element is always removed if the index you supply is valid, and an
ArrayIndexOutOfBound-
sException
is thrown if it isn't.
The
removeAll()
method accepts an argument of type
Collection<>
and removes elements from the
collection that you pass to the method if they are present in the vector. The method returns
true
if the
Vect-
or
object is changed by the operation — that is, if at least one element was removed. You could use this in
conjunction with the
subList()
method to remove a specific set of elements:
names.removeAll(names.subList(5,15));
This removes elements 5 to 14, inclusive, from the
Vector<String>
object
names
, plus any duplicates
of those objects that are in the vector.
The
removeRange()
method expects two arguments, an index for the first element to be removed, and an
index to one past the last element to be removed. So you could apply this to names like this:
names.removeRange(5, 15);
This removes elements with index values from 5 to 14 inclusive. This is different from the previous state-
ment in that duplicates of these elements will not be removed.
The
retainAll()
method provides a backhanded removal mechanism. You pass a reference of type
Col-
lection<>
as the argument that contains the elements to be retained. Any elements not in the collection