Vector In JAVA : Complete Tutorials

Here is the complete tutorials for the Vector in Java. Here is the detail explanation of vector in Java.Basically vector is a class which implements the collection interface in Java.

VECTOR class in Java is one of the implemented class of List Interface.

Vectors Features 

  • The underlined data structure for Vector is Resizable Array or Growable Array.
  • Insertion order is preserved in Vectors.
  • Duplicate objects are allowed in Vectors.
  • Heterogeneous Objects are allowed in Vectors.
  • ‘null’ insertion is possible in Vectors.
  • Vector implements serializable, clonable and random access.
  • Every method of Vector class is synchronised, so vector objects are ‘Thread-Safe’.

Now, let’s talk about constructors of Vector class.


Constructor in Vector

1) Vector V = new Vector();

This constructor (i.e. Vector()) creates a new blank vector with initial capacity of 10.

Now, say that vector is filled with 10 element. Now, if we are going to add the 11th element a new vector will be created with capacity just double of the size of the previous vector. That means the new vector will be of size 20. Again if that vector is also filled with 20 elements, and we are going to insert the 21st element, a new vector will be created with initial capacity 40 (i.e. double in the size of previous vector).

So,     New Capacity = Current Capacity * 2

Now, at the beginning if we know that how many elements we are going to insert and if that number is more than 10 then the above way of creating a vector (i.e. first we create a vector of capacity 10, then increase its size to 20, then again increase its size to 40 in this way) of the required size is the worst approach. That’s why, a new constructor was created.


2) Vector V = new Vector(int initial_capacity)

This constructor will create a new blank vector of the defined initial capacity.

As example, Vector V = new Vector(80) will create a vector of size 80.

Now, let say we have created a vector of capacity 1000 and we have filled it with 1000 elements. Now, we have to enter only 6 elements. When we will insert the first element after inserting 1000 element immediately a new vector of size 2000 will be created. But our total requirement is 1006. So remaining memory areas are wasted. To solve this problem, the next constructor was introduced.


3) Vector V = new Vector(int initial_capacity, int incremental_capacity)

By this constructor, a new vector will be created of the capacity of initial_capacity. And after filling that vector, the size of the new vector will be incremented by the incremental capacity. In simple words,

Vector V = new Vector(1000,6) will create a vector of size 1000. After filling up the vector with 1000 elements, when we will be inserting 1001th element, a new vector will be created with size 1006. After fulfilling that vector also, a new vector of size 1012 will be created. That means each time the size will be incremented by 6.

This type of flexibility is not there is ArrayList.


The next constructor is:

4) Vector V = new Vector(Collection C)

This constructor converts any Collection object to a Vector object.


Java Code to demonstrate constructors

// constructors in Vectors
		// creates empty vector object with intial size 10
		Vector Vec = new Vector();
		 
		// creates empty vector of size 100
		Vector Vec1 = new Vector(100);
		
		// creates empty vector of size 100 and increment factor 5
		Vector Vec2 = new Vector(100,5);
		
		// creates equivalent vector of Arraylist
		ArrayList array_list = new ArrayList(100);
		Vector Vec3 = new Vector(array_list);

Vector methods

Methods to add objects:

  • add(Object obj) :

This is method from Collection (Interface) which we can use in vectors to add objects. In this case object obj will be added.

  • add(int index, Object obj) This is method from List which we can use in vector to add objects. In this case object obj will be added at the specified index.
  • addElement(Object obj) : This method is only for vectors to add element (Objects). In this case Object obj will be added.

Methods to remove objects:

  • remove(Object obj) : This method is from Collection (Interface) to remove objects from a vector. In this case object obj will be removed.
  • remove(int index) : This method is from List to remove elements from a vector at a particular index. In this case element at the specified index will be removed.
  • clear() : This method is from Collection (interface) to clear a vector totally. There will not be a single element in that vector.
  • removeElement(Object obj) : This method is only for vectors to remove an object. In this case object obj will be removed.
  • removeElementAt(int index) : This is also a vector specific method to remove element at a particular index. In this case element at the specified index will be removed.
  • removeAllElement() : This is also a vector specific method to clear a vector totally, there will not be a single element present in the vector.

Methods to get Objects:

  • Object get(int index) :This method is from List to get the object from the specified index.
  • Object firstElement() :This is a vector specific method which returns the first object (element) from a vector.
  • Object lastElement() :This method is also for vectors which returns the last object (element) from a vector.
  • Object elementAt(int index) :This is also a vector specific method which returns the object from a particular index.

Other methods:

  • int size() :This method returns the number of elements present in a vector.
  • int capacity() :This method returns the total capacity of a vector.
  • Enumeration elements() :This method is used to get objects from a vector one by one.

Note : There is no such capacity() method in ArrayList which is introduced after vector because java is high level language program and memory management is not required.

		// creates empty vector object with intial size 10
		Vector Vec = new Vector();
		System.out.println(Vec.capacity());// initial size 10
		// creates empty vector of size 100

		Vec.add("a"); //add 1st element
		Vec.add("b"); //add 2ndt element
		Vec.add("c"); //add 3rd element
		Vec.add("d"); //add 4th element
		Vec.add("e"); //add 5th element
		Vec.add("f"); //add 6th element
		Vec.add("g"); //add 7th element
		Vec.add("i"); //add 8th element
		Vec.add("j"); //add 9th element
		Vec.add("k"); //add 10th element
		
		System.out.println(Vec.capacity());// initial size 10
		Vec.add("l"); //add 11th element
		System.out.println(Vec.capacity());// size 20 now ( 10 *2 )
		
		Vec.remove(3); // remove element at 3r position -> d
		System.out.println(Vec);// Output -> [a, b, c, e, f, g, i, j, k, l]
		
		// get first element
		System.out.println(Vec.firstElement());// Output -> a
		
		// Enumeration  in Vector
		Enumeration vec_enum = Vec.elements();
		while(vec_enum.hasMoreElements())
		{
			System.out.println(vec_enum.nextElement());
		}
		
		// Output -> [a, b, c, e, f, g, i, j, k, l]
	}

Now let’s perform a small program on vectors for better understanding;

import java.util.*;
class VectorDemo
{
	public static void main(String[] args)
	{
		int i=0;
		Vector vec = new Vector();
		// A blank vector created with initial capacity 10;
		System.out.println(“Initial Capacity : ”+(vec.capacity()));
		//Output : Initial Capacity : 10;
		for (i=0;i<10;i++)
		{
			if(i%2==0) // That is if ‘i’ is even
				vec.add(i);
			else 		//That is if ‘i’ is odd
				vec.addElement(i);
		}
		System.out.println(vec);
		// Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
		System.out.println(“Now number of elements in vector : ”+(vec.size()));			// Output : Now number of elements in vector : 10;
		System.out.println(“Current Capacity : ”+(vec.capacity()));
		//Output : Current Capacity : 10;
		vec.add(11);
		System.out.println(“Current Capacity : ”+(vec.capacity()));
		// Output : Current Capacity : 20;
		System.out.println(vec);
		// Output : [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11]
	}
}

Now if we compile this code, it will compile successfully but will give the following warnings:

Note: VectorDemo.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

These warning are there due to generics, if we modify the vector declaration statement according to generics we will not get any warnings then.

Now if we run our code the following output will be produced :

Output

Initial Capacity : 10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Now number of elements in vector : 10
Current Capacity : 10
Current Capacity : 20
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11]

Hope this articles covers your all doubt about the Vector class in java and covered all java interview questions.

hope you like this article on the Vector Class in JAVA.