Java – Vector Example


Vector class is in java.util package of java. Vector is dynamic array which can grow automatically according to the required need. Vector does not require any fix dimension like String array and int array. Vector contains many useful methods. To add element in Vector, we can use add() method of vector class. To add elements at fix position, we have to use add(index, object) method.

To get value from Vector, Vector provides get() method and Vector size() method. Size() method returns total number of elements in Vector.

Vector is synchronized, ArrayList is not

Vector Example

import java.util.Vector;

public class VectorExample {

    public static void main(String[] args) {

        Vector<String> vc=new Vector<String>();

        //    <E> Element type of Vector e.g. String, Integer, Object ...

        // add vector elements
        vc.add("Vector Object 1");
        vc.add("Vector Object 2");
        vc.add("Vector Object 3");
        vc.add("Vector Object 4");
        vc.add("Vector Object 5");

        // add vector element at index
        vc.add(3, "Element at fix position");

        // vc.size() inform number of elements in Vector
        System.out.println("Vector Size :"+vc.size());

        // get elements of Vector 
        for(int i=0;i<vc.size();i++)
            System.out.println("Vector Element "+i+" :"+vc.get(i));



5 Responses to “Java – Vector Example”

  1. Nudge says:

    There’s a bug: why You write “System.out.println(“Vector Size :”+vc.size());” and then the output is “ArrayList Size :6″???
    Sound me something wrong :S

  2. poonam says:

    Vector progam is working fine when i checked, 6 times vector added so printing 6, tell us the bug in detail

  3. dcpatton says:

    The problem is that you inserted the elements as the following strings:

    vc.add(“Vector Element #”);

    and you printed them out with vc.get(i) which led to them being the following strings:

    Vector Object #

    You example would have been clearer if you inserted them as “Vector Object #”

  4. Douglas says:

    Thank you for showing us something about the Vector class. Due to the newer Collections framework it is hard to find examples of Vector class usage. C++ programmers are very familiar with Vector. Keep that in mind as I understand Java was based on C.
    For the most part, Vector may still be used but many think that it should be deprecated. I am of no opinion here as I have not used it enough yet to understand the inner workings of this class. Many other classes in Java implement Vector and I would be very interested to see how that is done.
    Is Vector type-safe? Is it thread-safe? These are questions that I hope more qualified persons can answer.

    One quick note: I have used Vector with an iterator in C++. How else could it be done in Java?
    Try-catch statements are one big advantage that we have in Java and could be used in case of need for debugging. Also I tried this just to see the result: System.out.println(vc);

  5. dabba says:

    what about other vector operations?