Working with Vector class

Working with Vector class

Posted in : Core Java Posted on : November 2, 2010 at 11:50 AM Comments : [ 0 ]

This section contains the detail about the Vector class in java.

Working with Vector class

Vector is defined in java.util.Vector class and it implements a dynamic array. It's working is similar to ArrayList but it is little a bit different from it. For instance, Vector is  synchronized and use many methods which are not the part of the collection framework. With the release of Java 2, Vector was reengineered to extend AbstractList and implement the List interface, so it now is fully compatible with collections.

Given below some of Vector constructor :

Constructors Description
Vector( )  It create a default vector, which has an initial size of 10. 
Vector(int size)  It creates a vector whose initial capacity is specified by size.
Vector(int size, int incr)  It 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.
Vector(Collection c)  It creates a vector that contains the elements of collection c.

Initially, all Vectors are defined with initial capacity. When this capacity is reached, the next time that you attempt to store an object in the vector, the vector automatically allocates space for that object plus extra room for additional objects. By allocating more than just the required memory, the vector reduces the number of allocations that must take place. The amount of extra space allocated during each reallocation is determined by the increment that you specify when you create the vector. If you don't specify an increment, the vector's size is doubled by each allocation cycle.

Vectors has following protected data members :

Data members  Description 
int capacityIncrement; The increment value is stored in capacityIncrement
int elementCount;  The number of elements currently in the vector is
stored in elementCount.
Object elementData[ ];  The array that holds the vector is stored in elementData.

Since Vector implements List, you can use a vector just like you use an ArrayList instance.


import java.util.*;

public class VectorDemo {
public static void main(String[] args) {
Vector<Object> vector = new Vector<Object>();
int primitiveType = 10;
Integer wrapperType = new Integer(20);
String str = "Ankit Kaushal";
vector.add(2, new Integer(30));
System.out.println("the elements of vector: " + vector);
System.out.println("The size of vector are: " + vector.size());
System.out.println("The elements at position 2 is: "
+ vector.elementAt(2));
System.out.println("The first element of vector is: "
+ vector.firstElement());
System.out.println("The last element of vector is: "
+ vector.lastElement());
Enumeration e = vector.elements();
System.out.println("The elements of vector: " + vector);
while (e.hasMoreElements()) {
System.out.println("The elements are: " + e.nextElement());

Description of the Code

Given below the methods, used in the program, with their description :

 Methods  Description   
add(Object o) It adds the element in the end of the Vector 
size() It gives the number of element in the vector. 
elementAt(int index) It returns the element at the specified index. 
firstElement()  It returns the element at the specified index. 
lastElement()  It returns  last element. 
removeElementAt(int index)  It deletes the element from the given index.
elements()  It returns an enumeration of the element.
hasMoreElements()  It checks if this enumeration contains more elements or not. 
nextElement() It checks the next element of the enumeration.

Output :

C:\Program Files\Java\jdk1.6.0_18\bin>java VectorDemo
the elements of vector: [10, 20, 30, Ankit Kaushal]
The size of vector are: 4
The elements at position 2 is: 30
The first element of vector is: 10
The last element of vector is: Ankit Kaushal
The elements of vector: [10, 20, Ankit Kaushal]
The elements are: 10
The elements are: 20
The elements are: Ankit Kaushal

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image

Tutorial Topics