dsa

Java Vector

In this tutorial, we will learn about the Vector, its methods and how to implement it in Java.

Vector class implements List interface and it allows us to create resizeable arrays.

Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains components that can be accessed using an integer index

Constructors in the Vector

To use methods of an Vector class we have to create objects of Vector which can be done by the constructors of the Vector.There are four constructors present in Vector :

  1. Vector(): This constructor is used to create an empty Vector with 10 as initial Capacity.It can be created as:
    Vector v = new Vector();
    pre>
  2. Vector(int size): This constructor is used to create an Vector having the given initial size.
    Vector v = new Vector(N);
    pre>
  3. Vector(int size ,int incr): Creates a vector whose initial capacity is specified by size and increment is specified by incr.
    Vector v = new Vector(N, incr);
    pre>
  4. Vector(Collection c): Creates a vector that contains the elements of collection c.

Methods of Vector

Apart from the methods inherited from its parent class/interface ,it has following methods :

MethodsDescription
void addElement(Object element): It inserts the element at the end of the Vector.
int capacity(): This method returns the current capacity of the vector.
int size(): It returns the current size of the vector.
void setSize(int size): It changes the existing size with the specified size.
boolean contains(Object element): This method checks whether the specified element is present in the Vector. If the element is been found it returns true else false.
boolean containsAll(Collection c): It returns true if all the elements of collection c are present in the Vector.
Object elementAt(int index): It returns the element present at the specified location in Vector.
Object firstElement(): It is used for getting the first element of the vector.
Object lastElement(): Returns the last element of the array.
Object get(int index): Returns the element at the specified index.
boolean isEmpty(): This method returns true if Vector doesn’t have any element.
boolean removeElement(Object element): Removes the specifed element from vector.
boolean removeAll(Collection c): It Removes all those elements from vector which are present in the Collection c.
void setElementAt(Object element, int index): It updates the element of specifed index with the given element.

Example


import java.util.*;

public class VectorDemo 
{
   	public static void main(String args[]) 
	{
      		// Creating a vector of initial capacity of 2
      		Vector vector = new Vector(2);

      		// Inserting elements to a vector
      		vector.addElement("Bhopal");
      		vector.addElement("Pune");
      		vector.addElement("Delhi");
      		vector.addElement("Jharkhand");

      		// printing the size and the increased capacity of vector
      		System.out.println("Size is: "+vector.size());
      		System.out.println("Default capacity increment is: "+vector.capacity());
		System.out.println("New Vector: " + vector);
      		vector.addElement("Haryana");
      		vector.addElement("Jabalpur");
      		vector.addElement("Patna");

      		// printing the size and increased capacity after two insertions
      		System.out.println("Size after addition: "+vector.size());
      		System.out.println("Capacity after increment is: "+vector.capacity());
		System.out.println("New Vector: " + vector);
		// removing elements of a vector
		String city1 = vector.remove(1);
		String city4 = vector.remove(4);
        	System.out.println("Removed Element: " + city1+" and "+city4);
        	System.out.println("New Vector: " + vector);

        	// Using clear()
        	vector.clear();
        	System.out.println("Vector after clear(): " + vector);
      		
   	}
}

Output:

Size is: 4                                                                                                                     
Default capacity increment is: 4                                                                                               
New Vector: [Bhopal, Pune, Delhi, Jharkhand]                                                                                   
Size after addition: 7                                                                                                         
Capacity after increment is: 8                                                                                                 
New Vector: [Bhopal, Pune, Delhi, Jharkhand, Haryana, Jabalpur, Patna]                                                         
Removed Element: Pune and Jabalpur                                                                                             
New Vector: [Bhopal, Delhi, Jharkhand, Haryana, Patna]                                                                         
Vector after clear(): []