1
0

Vectors in Java

The legacy class Vector also supports dynamic arrays. Vector implements a dynamic array. It is similar to ArrayList, but with two differences: Vector is synchronized, and it contains many legacy methods that are not part of the collections framework. With the release of Java 2, Vector was re-engineered to extend AbstractList and implement the List interface, so it now is fully compatible with collections. Java does not support the concept of variable arguments to a function. This feature can be achieved in Java through the Vector class contained in java.util package. This class can create a generic dynamic array known as a vector that can hold objects of any type and number. The objects do not have to be homogeneous. Arrays can be easily implemented as vectors.

Vector Constructors #

Here are the Vector constructors:

Vector()
Vector(int size)
Vector(int size, int incr)
Vector(Collection c)

The first form creates a default vector, which has an initial size of 10 elements.

The second form creates a vector whose initial capacity is specified by size.

The third form creates a vector whose initial capacity is specified by size and whose increment is specified by incr. The increment specifies the number of elements to allocate each time that a vector is resized upward.

The fourth form creates a vector that contains the elements of collection c. This constructor was added by Java 2.

Note that a vector can be easily declared without specifying any size explicitly. A vector can accommodate an unknown number of items. Even, when size is specified, this can be overlooked and a different number of items may be put into the vector. Vectors possess a number of advantages over arrays i.e.

  • It is convenient to use vectors to store objects.
  • A vector can be used to store a list of objects that may vary in size.
  • We can add and delete objects from the list as and when required. A vector can contain different types of objects.
  • The major constraint of the vector is that we cannot directly store simple data types in a vector; we can store only objects. Therefore, we need to convert simple types to objects by using wrapper classes.

Vector Methods #

The Vector class supports a number of methods that can be used to manipulate the vectors and these are:

Let vect is an object of Vector class.

vect.addElement(item);          // add item to the end of vect
vect.elementAt(n);              // return name of nth object in vect
vect.size();                    // return size of vect
vect.removeElement(item);       // retun item from vect
vect.removeElementAt(n);        // remove nth object from vect
vect.removeAllElements();       // remove all objects from vect
vect.copyInto(array);           // copy all object to array
vect.insertElementAt(item,n);   // insert item at nth location in vect

Program to implement vectors in Java

import java.util.*;
class VectorImp
{
    public static void main(String str[])
    {
        Vector vect = new Vector();
        int len = str.length;
        for(int i=0; i<len; i++)
        {
            vect.addElement(str[i]);
        }
        vect.insertElementAt("RAJ",3);
        int s = vect.size();
        String arr[] = new String[s];
        vect.copyInto(arr);
        System.out.println("List of Names: ");
        for(int i=0; i<len; i++)
        {
            System.out.println(arr[i]);
        }
    }
}

Program to Demonstrate various Vector operations.

import java.util.*;
class VectorDemo
{
    public static void main(String args[])
    {
        Vector v = new Vector(3,2);
        System.out.println("Initial size: " + v.size());
        System.out.println("Initial capacity: " +v.capacity());
        v.addElement(new Integer(1));
        v.addElement(new Integer(2));
        v.addElement(new Integer(3));
        v.addElement(new Integer(4));
        System.out.println("Capacity after four additions: "+v.capacity());
        v.addElement(new Double(5.45));
        System.out.println("Current capacity: "+v.capacity());
        v.addElement(new Double(6.08));
        v.addElement(new Integer(7));
        System.out.println("Current capacity: "+v.capacity());
        v.addElement(new Float(9.4));
        v.addElement(new Integer(10));
        System.out.println("Current capacity: "+v.capacity());
        v.addElement(new Integer(11));
        v.addElement(new Integer(12));
        System.out.println("First element: "+(Integer)v.firstElement());
        System.out.println("Last element: "+(Integer)v.lastElement());
        if(v.contains(new Integer(3)))
        System.out.println("Vector contains 3.");
        Enumeration vEnum = v.elements();
        System.out.println("\nElements in vector:");
        while(vEnum.hasMoreElements())
            System.out.print(vEnum.nextElement() + " ");
        System.out.println();
    }
}

Leave a Reply