Java Dynamic Sized Arrays - Vector Class Example

Vector is an array like structure whose size could be changed dynamically. We can keep on adding elements to it without worrying about its size. Whenever there is an addition of new element to the Vector, it checks the capacity and if required it reallocates a new array with new size. This requires copying the existing data to the new array. The Vector class is found in the java.util package.
Vector is also similar to java.util.ArrayList of the Collection Framework, with few differences - that the methods of Vector are synchronized and vectors class has legacy method that are not part of collections framework e.g. addElement() and elementAt().

An example program with few methods of Vector Class:
package com.examples;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.Vector;

public class VectorExample1 {
 public static void main(String[] args) {
  //Default constructor of Vector
  Vector vec1 = new Vector();
  System.out.println("Initial Size: " + vec1.size() + ", Initial Capacity: " + vec1.capacity());
  System.out.println("Is vector empty: " + vec1.isEmpty());
  vec1.add(new Integer(5));
  vec1.addElement(new Integer(10));
  System.out.println("After adding 2 elements in vector, vec1 is: " + vec1);
  System.out.println("Current Size: " + vec1.size() + ", Capacity: " + vec1.capacity());
  System.out.println("Clearing the vector... ");
  vec1.clear();
  System.out.println("After clearing - Size: " + vec1.size() + " Capacity: " + vec1.capacity());
  
  //Constructor with initital size 5 and increment capacity 2
  Vector vec2 = new Vector(5,2);
  System.out.println("********************************\n");
  
  //Constructor with only initital size, and type argument
  Vector vec3 = new Vector(2);
  vec3.add(new Integer(5));
  vec3.add(new Integer(15));
  vec3.add(new Integer(25));
  vec3.add(new Integer(45));
  vec3.add(3, new Integer(35)); //add at index postion 3
  System.out.print("Elements in vector vec3: ");
  for (Integer v: vec3) System.out.print(v + " "); 
  System.out.println();
  System.out.println("Get by index, at position 2: " + vec3.get(2));
  vec3.remove(2);//Remove element at index 2
  System.out.println("Get by index after remove, at position 2: " + vec3.get(2));
  System.out.println("Iterating through vector vec3 using Enumeration:");
  Enumeration en = vec3.elements();
  while(en.hasMoreElements()) System.out.print(en.nextElement() + " ");
  System.out.println("\n********************************\n");

  //Cloning vec3 to vec4
  Vector vec4 = (Vector) vec3.clone();
  System.out.println("First elemt of vec4(cloned): " + vec4.firstElement());
  System.out.println("Last elemt of vec4(cloned): " + vec4.lastElement());
  System.out.println("Iterating through vector vec4 using Iterator:");
  Iterator vit = vec4.iterator(); 
  while(vit.hasNext()) System.out.print(vit.next() + " "); 
 }
}

Below is the output of the program:
Initial Size: 0, Initial Capacity: 10
Is vector empty: true
After adding 2 elements in vector, vec1 is: [5, 10]
Current Size: 2, Capacity: 10
Clearing the vector... 
After clearing - Size: 0 Capacity: 10
********************************

Elements in vector vec3: 5 15 25 35 45 
Get by index, at position 2: 25
Get by index after remove, at position 2: 35
Iterating through vector vec3 using Enumeration:
5 15 35 45 
********************************

First elemt of vec4(cloned): 5
Last elemt of vec4(cloned): 45
Iterating through vector vec4 using Iterator:
5 15 35 45 

No comments:

Post a Comment