Java Vector Class

Share:
Java Vector Class | Java Collection Vector Class


  1. Vector is a class present in Java.util  package, it is very similar to ArrayList but Vector is synchronized and ArrayList is not synchronized. 
  2. The Vector class underlying data structure is growable array, Vector class basically falls in legacy classes but now it is fully compatible with collection’s.
  3. Vector implements a dynamic array that means it can grow as per requirement.
  4. Like an array, it contains component that can be accessed by an index.
  5. Vector class is similar to ArrayList but ArrayList is not synchronized and Vector class is synchronized.
  6. Vector class is legacy and have some legacy methods that collection does not contain.
  7. Vector class extends AbstractList  and implements List interface in hierarchy.

Points about Vector class need to be remembered

  • 1.     Insertion order is preserved.
  • 2.     Duplicates are allowed.
  • 3.     Null is allowed.
  • 4.     Heterogeneous elements are allowed unlike array.
  • 5.      Implements List<E>, RandomAccess, Cloneable and Serilizable.
  • 6.     It is thread safe because methods are synchronized.
Advertisement
Vector Class Constructor
1) Vector v=new Vector();
It creates an empty Vector with initial capacity i.e., 10.
But when it reaches it max capacity then it creates a new Vector object with double of initial capacity, for example 10x2=20.

New Vector=Current Capacity x 2.

2) Vector v=new Vector(int c);
It creates a Vector object with specified initial capacity.

3)Vector v=new Vector(int  c , int  increment );
It creates a Vector object with specified initial capacity and with incremental capacity.

4) Vector v=new Vector(Collection c);
It creates an equivalent object for the given collection, means inter-conversion between collection object.

Now Vector Class Specific Methods
1) addElement(obj)
2) removeElement(obj)
3) removeElementAt(int i)
4) removeAllElement(int i)
Other methods are available by default from hierarchy.

Vector Class Example With Its Specific Methods, Using First Constructor.
import java.util.*;    
public class VectorMethods {
     public static void main(String[] args){
          //default constructor with 10 initial capacity
          Vector v=new Vector();
          v.addElement(null);
          v.addElement(1);
          v.addElement("one");
          v.addElement(2);
          v.addElement("two");
          System.out.println("After addElement "+v);
         
          //removing null
          v.removeElement(null);
          System.out.println("After removal of some elements "+v);
         
//removing element from 0(Zero) index i.e.,1 ->here v    contains[1, one, 2, two]
          v.removeElementAt(0);
          System.out.println("Now vector contains "+v);
         
//removing element from 1st index i.e.,2 ->here v contains[one, 2, two]
          v.removeElementAt(1);
          System.out.println("Now v contains "+v);
         
          //now removing all elements
          v.removeAllElements();
          System.out.println("Empty Vector "+v);
     }
}

Output:
After removal of some elements [1, one, 2, two]
Now vector contains [one, 2, two]
Now v contains [one, two]
Empty Vector []

Another Example Of Vector
In this example we have used methods add(obj) to add objects to end of the current vector ,
add(int  index , obj) to add specific element at specified index, addAll(Collection c) to copy all elements of vector to another  vector or list, add All(int index ,Collection c) to copy all elements of vector to another  vector or list at specified index and clear() method to clear the vector, see below example.
import java.util.*;    
public class VectorMethods{
public static void main(String[] args){
//default constructor
          Vector v=new Vector();
          v.add(1);
          v.add("one");
          v.add(2);
          v.add("two");
          System.out.println("v--->"+v);
//using add(int index, object)
          v.add(0,0);
          v.add(1,"zero");
          System.out.println("v--->"+v);
         
//creating another vector and copying all elements of v to v_add
          Vector v_add=new Vector();
//copying all elements of v to v_add using addAll(Collection c)
          v_add.addAll(v);
          System.out.println("v_add--->"+v_add);
         
//creating another vector
          Vector v_add_at=new Vector();
          v_add_at.add("x");
          v_add_at.add("y");
//copying all elements of v_ad to v_add_at using addAll(int index, Collection c)
          v_add_at.addAll(2, v_add);
          System.out.println("v_add_at--->"+v_add_at);
           
          v_add_at.clear();
          System.out.println("v_add_at after clear--->"+v_add_at);
     }
}

Output:
v--->[1, one, 2, two]
v--->[0, zero, 1, one, 2, two]
v_add--->[0, zero, 1, one, 2, two]
v_add_at--->[x, y, 0, zero, 1, one, 2, two]
v_add_at after clear--->[]

 Vector Example Using clone() and contains() Methods
In this example we used clone() method that returns the clone of this vector and contains() method to check whether a particular elements is present or not, it returns true if available or false if not available.
import java.util.*;    
public class VectorMethods {
     @SuppressWarnings({ "rawtypes", "unchecked" })
     public static void main(String[] args){
          //default constructor
          Vector v=new Vector();
          v.add("one");
          v.add("two");
          v.add("three");
          System.out.println("Normal v--->"+v);
         
         //default constructor
         Vector v_clone=new Vector();
         v_clone=(Vector)v.clone();
         System.out.println("Clone of v"+v_clone);
        
         //checking a particular element is present or not in v
         System.out.println(v.contains("one"));
         System.out.println(v.contains("four"));
          }
}

Output:
Normal v--->[one, two, three]
Clone of v[one, two, three]
true
false
Try with remaining methods.
Hope it will help you.
Please write comments if you find anything incorrect, or share more information on this topics.
Advertisement
  

No comments

You May Like Also

Programming Knowledge


Java JDK
Reversing String in Java
Java Generic Concept With Simple Example
Java Generic Objects Behavior In Non-Generic Area
Creating Connection With MySql Databse Using Java Application
Performing Database Operations Java MySql Insert Update Delete Select
Java Inserting Data in MySql Databse Table
Java Basic Login System Using MySql Database Table
JSP Login System Using MySql Databse
Jsp Servlet Login And Logout System Using Mysql Database Table
What is jdk in Java?
Exaplain inheritance in Java with example?
Exaplain polymorphism in Java with example?
Exaplain abstraction in Java with example?
Exaplain encapsulation in Java with example?
What is the use of this keyword in Java?
Explain ArrayList With Example
Explain LinkedList With Example
Explain Vector Class With Example
Explain Vector Class With Example
Generics in java with example
Behavior of generic objects
How can i pass generic class to a method in java
How to create a login system in jsp using mysql database?
How to create a login and logout system in jsp & servlet using mysql database?
How to connection a simple Java application with mysql database?
How to insert values in mysql databse usiong Java applications?
Perform insertion, retrieval, updatation and delete operations?
How to create a simple login system in java using mysql database?

PHP Login System Using MySql Database
PHP Complete Login System With Session And Logout Using MySql Database