Register Now

Login

Lost Password

Enter your email to reset your password.

BY Author

Java – Vector

Declaration

class Vector<E>

Vector extends AbstractList class & implements the List interface.

Vector is very much similar to ArrayList, but Vector is synchronized & ArrayList is non-synchronized.

List of Vector Constructors (CLICK HERE)

List of Vector Methods (CLICK HERE)

Non Generic Demo

In non-generic demo, we have used non-generic Vector class, constructor & methods. Therefore it can contain any type of elements (heterogeneous elements) as shown in program below:

Example

 import java.util.Vector;

 public class VectorDemo1 
 {
     public static void main(String args[])
     {
         Vector v = new Vector();
         v.addElement("ABC"); // String added
         v.addElement(9); // Integer added
         v.addElement('X'); // Character added
         v.addElement(7.99); // Float added
         v.add('Q'); // Character added
         System.out.println(v);
     }
 }

Output

[ABC, 9, X, 7.99, Q]

If we want Vector must contain elements of same type then we must use generic Vector.

Generic Demo

In the program below we have used generic Vector that can contain only Integer type of elements.
If we tried to insert element other than Integer, we will get compile time error.

Example – I

 import java.util.Vector;

 public class VectorGenDemo1 
 {
     public static void main(String args[])
     {
         Vector<Integer> v = new Vector<Integer>();
         System.out.println("Default Capacity = "+v.capacity());
         v.add(10);
         v.add(233);
         v.add(50);
      // v.add('A'););  // Character value is not allowed
                        // because v is of type Vector<Integer>
                        // it can't be used to refer to an
                        // object of Vector<Character>

         System.out.println(v);
     }
 }

Output

Default Capacity = 10
[10, 233, 50]

Example – II

 import java.util.Vector;

 public class VectorGenDemo2 
 {
     public static void main(String args[])
     {
         Vector<Integer> v = new Vector<Integer>();
         v.add(10);
         v.addElement(20);
         v.addElement(40);
         System.out.println(v);
         v.add(2, 30);
         System.out.println(v);
         System.out.println("Element at index 2 = "+v.get(2));
         System.out.println("Is vector v empty? = "+v.isEmpty());
         System.out.println("Size of vector v = "+v.size());
         System.out.println("First element = "+v.firstElement());
         System.out.println("Last element = "+v.lastElement());
     }
 }

Output

[10, 20, 40]
[10, 20, 30, 40]
Element at index 2 = 30
Is vector v empty? = false
Size of vector v = 4
First element = 10
Last element = 40

Example – III

 import java.util.Vector;

 class Employee 
 {
    int emp_id;
    String name;
    double salary;
	
    Employee(int i, String s, double d)
    {
        emp_id = i;
        name = s;
        salary = d;
    }
 }


 public class VectorGenDemo4 
 {
    public static void main(String args[])
    {
        Employee e1 = new Employee(10,"Tom",33333.34);
        Employee e2 = new Employee(20,"Jerry",44444.34);
        Employee e3 = new Employee(30,"Jack",55555.34);
        Employee e4 = new Employee(40,"Vishal",66666.34);
		
        Vector<Employee> v = new Vector<Employee>();
        v.addElement(e1);
        v.addElement(e2);
        v.addElement(e3);
        v.addElement(e4);
		
        for(Employee e:v)
        {
            System.out.println(e.emp_id+"  "+e.name+"  "+e.salary);
        }
    }
 }

Output

10 Tom 33333.34
20 Jerry 44444.34
30 Jack 55555.34
40 Vishal 66666.34

 

Leave a reply