Java Reference
In-Depth Information
The first call to add() inserts an element at index 1, between the “Pak” and “Han”
strings. The “Han” and “Inkster” strings are moved up an element in the vector to
accommodate the inserted “Park” string. The second call to add() inserts an element at
index 0, which is the beginning of the vector. All existing elements are moved up one
space in the vector to accommodate the inserted “Sorenstam” string. At this point, the
contents of the vector look like this:
“Sorenstam”
n
“Pak”
n
“Park”
n
“Han”
n
“Inkster”
n
The call to remove() removes the element at index 3, which is the “Han” string. The
resulting vector consists of the following strings:
“Sorenstam”
n
“Pak”
n
“Park”
n
“Inkster”
n
You can use the set() method to change a specific element:
v.set(1, “Kung”);
This method replaces the “Pak” string with the “Kung” string, resulting in the following
vector:
“Sorenstam”
n
“Kung”
n
“Park”
n
“Inkster”
n
If you want to clear out the vector completely, you can remove all the elements with the
clear() method:
v.clear();
The Vector class also provides some methods for working with elements without using
indexes. These methods search through the vector for a particular element. The first of
Search WWH ::




Custom Search