Java – Vector Class

  • The Vector class implements a growable array of objects. Similar to array.
  • It is similar to ArrayList, but with two differences:
  • Vector is synchronized.
  • Vector contains many legacy methods that are not part of the collections framework.
  • The elements of Vector can be accessed using an integer index.
  • The size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.
  • Vector is synchronized which means it is suitable for thread-safe operations
  • Vector implements List Interface. Like ArrayList it also maintains insertion order
  • it is synchronized and due to which it gives poor performance in searching, adding, delete and update of its elements.

 

class Vector <T>

We can create Vector for storing String type elements as follows:

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

We can create Vector for storing Integer type elements as follows:

Vector <Integer> hs=new Vector <Integer> ();


Constructors of Vector class as show below:

  • Vector( ): This constructor creates a default vector, which has an initial size of 10
  • Vector(int size): This constructor is use to create vector of specific  size .They are accept an argument that equals to the required size.
  • Vector(int size, int incr): This constructor 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) :creates a vector that contains the elements of collection c

 

 

 

Methods of Vector Class:

  1. void addElement(Object element): It inserts the element at the end of the Vector.
  2. int capacity(): This method returns the current capacity of the vector.
  3. int size(): It returns the current size of the vector.
  4. void setSize(int size): It changes the existing size with the specified size.
  5. 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.
  6. boolean containsAll(Collection c): It returns true if all the elements of collection c are present in the Vector.
  7. Object elementAt(int index): It returns the element present at the specified location in Vector.
  8. Object firstElement(): It is used for getting the first element of the vector.
  9. Object lastElement(): Returns the last element of the array.
  10. Object get(int index): Returns the element at the specified index.
  11. boolean isEmpty(): This method returns true if Vector doesn’t have any element.
  12. boolean removeElement(Object element): Removes the specifed element from vector.
  13. boolean removeAll(Collection c): It Removes all those elements from vector which are present in the Collection c.
  14. void setElementAt(Object element, int index): It updates the element of specifed index with the given element.

 

 

 Example of Vector in Java:

 

import java.util.Iterator;

 

import java.util.Vector;

 

public class MyVectorDemo {

 

 public static void main(String a[]){

 

 Vector<String> vct = new Vector<String>()

 

//adding elements to the end

 

vct.add("A");

 

vct.add("B");

 

vct.add("C");

 

vct.add("D");

 

Iterator<String> itr = vct.iterator();

 

 while(itr.hasNext()){

 

System.out.println(itr.next());

 

}

 

}}

Output:

A
B
C
D

Another example of Vector in Java:

import java.util.Vector;
 
public class MyVectorOperations {
 
    public static void main(String a[]){
        Vector<String> vct = new Vector<String>();
        //adding elements to the end
        vct.add("A");
        vct.add("B");
        vct.add("C");
        System.out.println(vct);
        //adding element at specified index
        vct.add(2,"D");
        System.out.println(vct);
        //getting elements by index
        System.out.println("Element at index 3 is: "+vct.get(3));
        //getting first element
        System.out.println("The first element of this vector is: "+vct.firstElement());
        //getting last element
        System.out.println("The last element of this vector is: "+vct.lastElement());
        //check vector is empty or not
        System.out.println(" vector empty? "+vct.isEmpty());
    }
}

Output:

[A, B, C]
[A, B, D, C]
Element at index 3 is: C
The first element of this vector is: A
The last element of this vector is: C
vector empty? false

 

2 thoughts on “Java – Vector Class

Leave a comment