- If you want to store instances, but you cannot predict how many there
will be, then use a vector, rather than an array.
- If you want to implement a queue or a push-down stack, then use a
vector.
- If you want to declare a vector variable and to create a vector
instance, then instantiate the following pattern:
Vector vector name = new Vector();
- If you want to add elements to the front or back of a vector, or to insert
an element into a vector, then instantiate one of the following
patterns:
vector name.insertElementAt(instance, 0)
vector name.addElement(instance)
vector name.insertElementAt(instance, index)
- If you want to retrieve an element from the front or back of a vector,
then instantiate one of the following patterns:
vector name.firstElement()
vector name.lastElement()
- If you want to retrieve an element from a vector, or to replace an element
in a vector as though that vector were an array, then instantiate one of the
following patterns:
vector name.elementAt(index)
vector name.setElementAt(index)
- If you want to know how many elements a vector contains, then
instantiate the following pattern:
vector name.size()
- If you want to use a vector element as a target for a method that belongs
to a particular class, and that vector element is known to be an
instance of the class, then cast the vector element to the
class by instantiating the following pattern:
((class name) vector element)
- If you want to perform a computation using every element of a vector in
turn, then instantiate the following pattern:
for (Iterator iterator variable = vector.iterator()
; iterator variable.hasNext()
;) {
... iterator variable.next() ...
}
- If you want to store
int
values in a vector, then wrap those
values in Integer
instances.