Java Vector

java vector

Java Vector is the implementation class to the List interface. It is almost equivalent to the ArrayList implementation class except the synchronization , Java Vector is thready safety ( Synchronized class). Stack is the child class to Vector class. Vector doesn’t comes under general purpose implementation classes , Vector class is also known to be called as Legacy class because it is introduced in the old version of Java 1.0. Let’s take a deeper look what actually a Java Vector class does.

class Vector

java.lang..object

      java.util.AbstractCollection<E> 

         java.util.AbstractList<E>

             java.util.Vector<E>

All implemented interfaces

Serializable , Cloneable , Iterable<E> , Collection<E> , List<E> , RandomAccess

Direct known sub classes

Stack


Java Vector 

public class Vector<E>
extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, Serializable

Like an ArrayList , Vector class also implements growable array of objects and the objects(elements) can be accessed with the index number associated with the element. The size of the Vector can grow and shrink according to the operations like add , remove are applied to the elements in the Vector.

Vector is good at storage management , as it maintains a capacity and the capacityIncrement functions. In other words we can specify the incrementation capacity by the capacityIncrement constructor. Vector array will be in vertical structure.

Java Vector inside diagram

Java Vector Bullet Points

  • Java vector implementation allows null and duplicate elements.
  • It is an ordered list ( in other words in which order the user inserts the elements the same order will be maintained ).
  • Elements are stored with the index number associated with them.
  • Vector class is synchronized i.e it is thread safety . It only allows one thread to modify the current list.
  • Like ArrayList Vector class is best recommended for retrieving of elements , adding and removing elements causes time performance.
  • Java Vector also implements RandomAccess marker interface , it supports time constant.
  • Java Vector class permits Collection interface methods , List interface methods as well it has it’s won methods.
  • In ArrayList the element are stored in horizontal structure where as in Vector the elements are stored in vertical structure.
  • The iterators returned by this class iterator and listIterator methods are fail-fast.

Java Vector Constructors

  1. public Vector()
  • Constructs an empty vector with default capacity 10.
  • Here the capacityIncrement is 0. Whenever the capacityIncrement is less than or equal to zero , the capacity of the Vector is doubled each time it needs to grow.
  • When the vector is full , then the increment will be done based on 
new capacity = 2 * current-capacity

2. public Vector(int initialcapacity)

  • Constructs an empty Vector with the specified initial capacity and increment will be based on
    new capacity = 2 * current-capacity

Exceptions

Throws

IllegalArgumentException : If the specified capacity is negative.

3. public Vector(int initialCapacity , int capacityIncrement)

  • Constructs an empty Vector with specified initial capacity and capacityIncrement.
  • Whenever the vector overflows than specified incrementation is done to the vector.

For Example , If the initial capacity of the vector is 10 and given the capacityIncrement is 5. When the vector is over filled , then the capacity will be incremented to 5 ( now total capacity is 15).

Exceptions

Throws

IllegalArgumentException : If the specified initial capacity and capacityIncrement is negative.

4. public Vector(Collection c)

  • Constructs a vector containing the specified elements in the collection ‘c’.
  • And the elements are inserted in the they are returned by their iterators.

Exceptions

NullPointerException : If the specified collection is null.

Java Vector Methods

To add elements

  1. public void addElement(Element e)
  • Adds the specified element to the end of the vector , increasing it’s size by one. The capacity of the vector is increased if it’s size becomes greater than the capacity.
  • Capacity is the total number of cells whereas size is the number of cells filled by the elements.
  • This method is equivalent to add method in Collection interface.

To remove elements 

  1. public boolean removeElement(Object o)
  • Removes the first lowest indexed occurrence of the specified object in the argument. 
  • If the object is found in the vector then each element in the vector where their index is greater than or equal to specified object index will shift downwards to have an index one smaller than before.

Java Vector removeElement() diagram

2. public void removeElementAt(int index)

  • Deletes the element at the specified index in the vector.
  • After deleting , the indexes which are greater than or equal to the specified index will shift downwards to have one smaller index than before ( above picture).
  • This method is equivalent to remove method in List interface it returns the removed object.

Exceptions

ArrayOutOfBoundsException : If the specified index is out of range. 

index < 0 || index >=size() if this is the case then this exception will occur

3. public void removeAllElements()

  • Removes all the elements present in the current vector and sets the size of the vector to zero.
  • This method is equivalent to clear method in List interface.

 

To get the elements

1.public E elementAt(int index)

  • Returns the element(component) at the specified index in the vector.
  • This method is identical to get method in List interface.

Exceptions

ArrayOutOfBoundsException : If the specified index is out of range. 

index < 0 || index >=size() if this is the case then this exception will occur

2. public E firstElement()

  • Returns the first element at index 0 of the current vector.
  • Throws NoSuchElementException , if the vector has no components

3. public E lastElement()

  • Returns the last element of the current vector . 
  • In other words it returns the element at index size() – 1
  • Throws NoSuchElementException if the vector contains no elements.

Java Vector Example 

import java.util.*;
 
public class VectorExample
{
 public static void main(Stirng[] args)
 {
 // creating an empty vector with capacity 5 and increment 2
 Vector v = new Vector(5,2);
 System.out.println("Elements in the vector v (size) : " +v.size());
 System.out.println("Total capacity to hold elements : " v.capacity());
 // adding elements to the vector 'v'
 v.addElement(1);
 v.addElement(2);
 V.addElement(3);
 v.addElement(4);
 v.addElement(5);
 V.addElement(6);//adding extra element to the vector to show the increasedcapacity
 //now the size of the vector 'v'
 System.out.println("The elements inside the vector(size) : v.size());
 //checking the capacity of the vector after adding the extra element
 System.out.println("The capacity after adding the extra element is:"v.capacity());
 //adding another element 
 v.addElement(4); //adding duplicate element
 //checking the size of the vector
 System.out.println("The size of the vector : " v.size());
 //checking the current capacity
 System.out.println("Current capacity :" v.capacity());
 //printing first element in the vector
  System.out.println("First element in vector v is : v.firstElement());
 //printing last element in the vector v
  System.out.println("Last element in vector v is : v.lastElement());
 //checking whether the element is present or not
  if(v.contains(4))
   {
      System.out.println(" Vector contains '4');
   }
 //iterating all the elements in the vector to print
  Enumerate venum = new v.elements();
   System.out.println("elements in vector v : ");
  while(venum.hasMoreElements())
   System.out.print(venum.nextElement() + " ");
   System.out.println();
}
}
 
 

OUTPUT

The size of the vector 'v' is : 0
the capacity of the vector v is : 5
The size of the vector after adding elements is : 5
The capacity of the vector after adding 5 elements :5
the Capacity of the vector after adding the extra element : 7
the size of the vector after adding the extra element : 6
1
4
Vector 4 is present
the elements in vector v are : 
1 2 3 4 5 4 

 


 

2 thoughts on “Java Vector

  1. I am now not positive where you’re getting your info, however great topic. I must spend a while studying much more or figuring out more. Thanks for fantastic info I was looking for this info for my mission.

  2. I have been surfing online more than three hours today, yet I never found any interesting article like yours. It’s pretty worth enough for me. In my opinion, if all web owners and bloggers made good content as you did, the internet will be much more useful than ever before.

Leave a Reply

Your email address will not be published. Required fields are marked *