by Dinesh Thakur Category: array

The Vector object store objects of any kind are extremely  although flexible operations on objects of this class are slower than  operations on common vectors (arrays). The capacity of the vector is the maximum amount of  elements that it can hold at a given instant. Its size corresponds to  number of elements actually stored. Having your whole capacity occupied  (size = capacity), adding a new element causes the automatic increase  capacity vector. If an increment is specified via its constructor, this increase occurs with this granularity, otherwise the capacity is doubled.

The main constructors and methods of java.util.Vector class are:



Vector ()

Constructs an initially empty Vector object.

Vector (int)

Constructs a Vector with the ability indicated.

Vector (int, int)

Constructs a Vector with the ability and capacity increase indicated.

addElement (Object)

Adds the object to the vector.

capacity ()

Returns the current capacity of the vector.

elementAt (int)

Returns the object contained in the given position.

insertElementAt (Object, int)

Inserts the given object at the position indicated.

removeAllElements ()

Removes all elements of the vector.

removeElementAt (Int)

Removes the element from the indicated position.

setElementAt (Object, int)

Replaces the element at the position indicated object provided.

setSize (int)

Specifies the capacity of the vector.

size ()

Returns the current number of elements contained in vector.

trim (int)

reduces the ability of the vector to the number of elements in the vector.


import java.util.*;

class JavaExampleVector
    public static void main(String as[])
        Vector Vctr = new Vector(5);
        System.out.println("Capacity: " + Vctr.capacity());
        Vctr.addElement(new Integer(0));
        Vctr.addElement(new Integer(1));
        Vctr.addElement(new Integer(2));
        Vctr.addElement(new Integer(3));
        Vctr.addElement(new Integer(4));
        Vctr.addElement(new Integer(5));
        Vctr.addElement(new Integer(6));
        Vctr.addElement(new Double(3.14159));
        Vctr.addElement(new Float(3.14159));
        System.out.println("Capacity: " + Vctr.capacity());
        System.out.println("Size: " + Vctr.size());
        System.out.println("First item: " + (Integer) Vctr.firstElement());
        System.out.println("Last item: " + (Float) Vctr.lastElement());
        if(Vctr.contains(new Integer(3)))
            System.out.println("Found a 3.");

Java Vector Example

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.

Search Content

Popular Article