Vector Example in Java

Vector can be used to implement dynamic Array. It can accommodate any number and any type of objects. It is used to store various objects into a single unit.

 

Vector class provides a variety of methods that can be used to perform different operations like addElement(object),removeElement(object),insertElementAt(),etc

import java.util.*;

public class VectorTest extends Vector

{

                public VectorTest()

                {

                super(1,1);

                }

                public void addInteger(int n)

                {

                                addElement(new Integer(n));

                }

                public void addString(String s)

                {

                                addElement(s);

                }

                public void addIntegerat(int n)

                {

                                insertElementAt(new Integer(n),1);

                }

                public void displayVector()

                {

                Object object;

                int length = size();

                System.out.println("Number of Vector elements:  "+length);

                System.out.println("They are:  ");

               

                for(int i=0;i<length;i++)

                {

                object = elementAt(i);

                System.out.println(object.toString());

                }

}

public static void main(String a[])

{

                VectorTest vector=new VectorTest();

                int num=100;

                String string=new String("XCnotes is my site");

                vector.addInteger(num);

                vector.addString(string);

                vector.displayVector();

                vector.addIntegerat(1111);

                vector.displayVector();

}

}

   Output.....        

Vector in Java    

About Us

Rekha SetiaRekha Setia is a passionate blogger of Extra Computer Notes. if you have any ideas or any request me @