Vectors

Since most programs involve gathering, manipulating and presenting information, it is important to know ways to organize the data objects we have. Java supports a number of different container mechanisms. Classes are the simplest kind of container. Each instance of a class can hold a variety of data values and methods to act on them. To deal with larger collections of objects, we will look at vectors.

A vector is related to an array. The prime difference is that while arrays are fixed in length, vectors can grow and shrink. Vectors also don't contain a particular type like arrays. A vector holds a collection of objects, of whatever type. This means that a Vector can hold different objects in different slots. Also, each element in a Vector is an Object reference not the actual object. Since all Java classes are Objects, this is why you can store anything in a Vector without having to say ahead of time what it is. However, when you retrieve something from a Vector, you have to cast it to the type you want it to be. Here is a short example that shows this characteristic of Vectors. It involves a Vector of simple objects.


// This is just a simple class used in the vector example

public class foo
{
    private int value;

    public foo() { value = 0; }
    public foo(int x) { value = x;}

    public void setValue(int y) { value = y; }
    public int getValue() { return value; }
}
And here is the driver class that does all the interesting things.

import foo;
import java.util.*;
/**
 * driver (main) for small vector example
 * 
 * @author (Kent Archie) 
 * @version (1/25/2002)
 */
public class vector
{   
    private static Vector v;
    /**
     * main
     * 
     * @param  args   command line arguments
     * @return     nothing 
     */
    public static void main(String[] args)
    {
        // create vector with 6 slots to start
        v = new Vector(6); // just references created, no objects yet
        // can't use size() here, nothing in it yet
        for(int i=0; i< 6; i++)  
            v.insertElementAt(new foo(i),i);

        printVector("Original Vector");
        incrVector(3);  // add 3 to each foo
        printVector("After increment");
        v.addElement(new foo(42));
        v.removeElementAt(3);
        printVector("After add and remove");
    } // main
    
    // use an iterator to print the vector
    private static void printVector(String str)
    {
        Iterator iter = v.iterator();  // start the iterator
        int i = 0;
        Object obj;
        foo f;
        System.out.println(str);
        while(iter.hasNext()) {
            obj = iter.next();
            f = (foo) obj;
            System.out.println("V(" + i + ") = " + f.getValue());
//            System.out.println("V(" + i + ") = " + ( (foo)v.elementAt(i)).getValue());
            i++;
        } // while
     } // printVector
    
     // add n to each foo in the vector
     private static void incrVector(int n)
     {
        for(int i=0; i < v.size(); i++) {
            ( (foo)v.elementAt(i)).setValue( ( (foo) v.elementAt(i)).getValue() + n);
        } // for
     } // incrVector
} // vector example driver
The code for this is in Vector example. Here is a link to a bigger example that implements high precision arithmetic using a Vector.