Vector
class. The Vector
class is an example of what is called a container
. Other
container
classes are Stack
(for implementing last-in,
first-out sequence) and Hashtable
(for an associative array).
Characteristic of Java container classes is that they automatically resize
themselves so that you can put in any number of objects. Furthermore, a Java
container looses all type information and simply holds objects of
type Object
. It is left to the programmer to remember what he or
she actually puts into the container and to perform a cast to the correct type
before using the elements.
In this section you will only learn the basics of vectors in Java.
Vector
class from the java.util
package.
To create a vector, use three steps:import java.util.Vector; // load Vector package
Vector v; // declare a vector object
v = new Vector(); // create an empty vector objectYou may also add a number to specify the (initial) capacity of the object, e.g.,
v = new Vector(5); // create a vector object with initial capacity 5
addElement
method.
v.addElement(new Integer(1)); // add first vector element v.addElement(new Float(1.9999)); // add another vector element for (int i=2; i<10; i++) { int lastInt = ((Number) v.lastElement()).intValue(); v.addElement(new Integer(i + lastInt)); } // recursively add more elements
would printSystem.out.println(v);
on the computer screen. To understand this result, see that at each step in the recursion we select the last element of the vector and consider it as a[1, 1.9999, 3, 6, 10, 15, 21, 28, 36, 45]
Number
to
which we can apply the intValue
method. Apparently, the
intValue
of Float(1.9999)
is 1
.
size
method returns the current number of elements in the
vector. In the above example, the statement
returns the current size of the vector (10), i.e., the number of accessible components. This number can grow and shrink as needed. It may differ from the capacity of the vector. For example, after the two commandsv.size();
we have deleted twice the first element of the vector. The size of the vector has become 8, but its capacity is still 10. Only after application of thev.removeElementAt(0); v.removeElementAt(0);
trimToSize
method, the capacity
has shrunk to 8 as well.
To get a specific element from a vector, use the elementAt
method.
An ArrayIndexOutOfBoundsException
is thrown when
an invalid index is given. You can use this method together with
size
method to step in a for
-loop through all
elements. For example, with the contents of the current vector v
the statement
will print on the computer screenfor (int i=0; i<v.size(); i++) { System.out.println("v[" + i + "] = " + v.elementAt(i)); }
v[0] = 3 v[1] = 6 v[2] = 10 v[3] = 15 v[4] = 21 v[5] = 28 v[6] = 36 v[7] = 45
An alternative way of stepping through vectors is
provided by the Enumeration
interface.
You can look up the source code of the Enumeration
interface in the development kit; it looks as follows:
So, these are the two methods that you can use to iterate through the set of values. The following two skeletons of Java code can be used (without any difference in efficiency):public interface Enumeration { boolean hasMoreElements(); Object nextElement(); }
while
-loop
Enumeration e = vector.elements(); // get all vector elements while (e.hasMoreElements()) { // step through all vector elements Object obj = e.nextElement(); // work with the object ..... }
for
-loop
for (Enumeration e = vector.elements(); e.hasMoreElements();) { Object obj = e.nextElement(); // work with the object ..... }
To set an element at a specific index in a vector, use the
setElementAt
method.
In summary, the pleasant []
syntax of accessing and changing
array elements has been replaced by the elementAt
and
setElementAt
methods. The table below shows the difference in style.
array | vector |
---|---|
x = a[i]; |
x = v.elementAt(i); |
a[i] = x; |
v.setElementAt(x, i); |
Vector
class provides a method called clone
for copying the data of one vector into another.
After these lines, the variableVector u = new Vector(); Vector v = u.clone();
v
refers to a duplicate of the
object that u
refers to. You can manipulate this new vector without
changing the original vector, or the other way around.
The assignment of one vector to another via
the =
assignment operator would destroy elements in the
original vector while changing elements in the copy. This phenomenon
is the same as for arrays.
The following Java program illustrates this.
import java.util.Vector; public class cpTest { public static void main(String[] args) { // initialization of arrays Vector u = new Vector(); u.addElement(new Integer(1)); u.addElement(new Integer(0)); Vector v = (Vector) u.clone(); Vector w = u; // printing vectors System.out.println("initial vectors"); System.out.println("u = " + u + ", v = " + v + ", w = " + w); System.out.println(""); // // changing u // u.setElementAt(new Integer(2),0); // System.out.println("changing u; v stays the same; w changes as well"); System.out.println("u = " + u + ", v = " + v + ", w = " + w); System.out.println(""); // // changing v // v.setElementAt(new Integer(3),0); // System.out.println("changing v; u and w stay the same"); System.out.println("u = " + u + ", v = " + v + ", w = " + w); System.out.println(""); // // changing w // w.setElementAt(new Integer(4),0); // System.out.println("changing w; v stays the same; u changes as well"); System.out.println("u = " + u + ", v = " + v + ", w = " + w); System.out.println(""); } }
initial vectors u = [1, 0], v = [1, 0], w = [1, 0] changing u; v stays the same; w changes as well u = [2, 0], v = [1, 0], w = [2, 0] changing v; u and w stay the same u = [2, 0], v = [3, 0], w = [2, 0] changing w; v stays the same; u changes as well u = [4, 0], v = [3, 0], w = [4, 0]