Java Reference
In-Depth Information
The parameter to the method is of type
Collection
, so the objects in any list or set can be added. To
insert the collection objects at a particular position, you specify the index position as the first argument.
So to insert the objects in
myList
starting at index position
i
, you would write:
transactions.addAll(i, myList);
The object originally at position
i
, and objects originally to the right of position
i
, will all be shuffled to
the right to make room for the new objects. If the index value passed as the first argument is negative,
or is not less than the size of
transactions
, an
ArrayIndexOutOfBoundsException
object will
be thrown. Adding a collection will increase the size of the vector by the number of objects added.
Retrieving Objects from a Vector
As we saw in the simple example earlier, if you have the index for an element, you can obtain the
element at a particular position by using the
get()
method for the
Vector
. For the
transactions
vector you could write:
Transaction theTransaction = (Transaction)transactions.get(4);
This statement will retrieve the fifth element in the vector. Note that the explicit cast is essential here. If you
don't cast the object returned to the type of the variable that you're using to store it, you'll get an error.
Of course, this is where an object of an incorrect type in the
Vector
will cause a problem. If the object
returned here isn't of type
Transaction
, an exception will be thrown. Although you could always store it in
a variable of type
Object
, you should always cast it to the original or most appropriate type. Note that the
get()
method will throw an exception of type
ArrayIndexOutOfBoundsException
if the argument is
an illegal index value. The index must be non-negative and less than the size of the vector.
You can retrieve the first element in a
Vector
by using the
firstElement()
method, which returns
the object stored as type
Object
. For example:
Transaction theTransaction = (Transaction)transactions.firstElement();
You can also retrieve the last element in a
Vector
by using the method
lastElement()
in a similar
manner. However, a vector has a flavor of a list about it, and if you want to process the objects in your
vector like a list, you can obtain an iterator.
Accessing Elements in a Vector through an Iterator
You can also obtain all the elements in a
Vector
object by using an
Iterator
object that you obtain from
the
Vector
object. In most instances this will be the preferred way of accessing the elements in a
vector
.
You obtain a reference to an iterator by calling the
iterator()
method for the
Vector
object:
Iterator theData = names.iterator();
The method
iterator()
returns an
Iterator
object that you can use to iterate through all the
elements in the
Vector
object.
Y
ou can now process them serially using the methods defined for
Iterator
class that we discussed earlier. For example, you could now output the elements from
names
in the last example we ran using the iterator: