4.1.2.2 Vectors


An array is a data type for collecting a fixed number of data of the same type. "Arrays" of variable sizes are provided by the 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.

  1. Creating Vectors
  2. Accessing, Changing, and Removing Elements
  3. Copying Vectors

Creating Vectors

Whenever you start working with vectors in Java, it is convenient to first import the Vector class from the java.util package.
import java.util.Vector;  // load Vector package
To create a vector, use three steps: In the above example, the statement
System.out.println(v);
would print
[1, 1.9999, 3, 6, 10, 15, 21, 28, 36, 45]
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 Number to which we can apply the intValue method. Apparently, the intValue of Float(1.9999) is 1.

Accessing, Changing, and Removing Elements

The size method returns the current number of elements in the vector. In the above example, the statement
v.size();  
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 commands
v.removeElementAt(0);
v.removeElementAt(0);
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 the 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

for (int i=0; i<v.size(); i++) {
   System.out.println("v[" + i + "] = " + v.elementAt(i));
}
will print on the computer screen
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:

public interface Enumeration {

    boolean hasMoreElements();

    Object nextElement();
    
}
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): This style of stepping through sets of values is also applicable to other containers such as stacks and hash tables.

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); 

Copying Vectors

The Vector class provides a method called clone for copying the data of one vector into another.
Vector u = new Vector();
Vector v = u.clone();
After these lines, the variable 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.

Java Code
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("");
  }
}
Output
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]