Java Made Simple: What is Vector Java with Example?


Sometimes, a data structure is preferred rather than an array in that data structure is growable and accepts dissimilar elements. Being growable dynamically, Vector is preferred when the number of elements to be added are not known earlier in advance or required to change the number of elements (modify the size). Vector is the generally preferred data structure (DS) in Java. Like any other DS, it is placed in java.util package. Now let us dig more on What is Vector Java ?
  1. Like other DS, Vector accepts elements in object form only. To say, Vector is a growable array (dynamic array) of objects, where Vector size can be grown or shrinked, and like an array, the Vector elements can be retrieved with index number.
  2. All the Vector methods are synchronized by default (like StringBuffer methods). That is, if one Vector object is executing a method, the other Vector object cannot use the same method. For this reason, Vector is most preferred in multithreaded environment.
  3. If multithreaded coding does not exist, prefer ArrayList where the methods are not synchronized. Due to synchronization, Vector is considerably slower than ArrayList. When to prefer ArrayList for Vector is elaborately explained Vector vs ArrayList.
  4. Vector, eventhough introduced with JDK 1.0 (all the DS introduced with JDK 1.0 are known as legacy classes), still it can make use of Collections framework methods as from JDK 1.2, Vector class in Java API implements List interface. See the class signature

    public class java.util.Vector extends java.util.AbstractList implements java.util.List, java.util.RandomAccess, java.lang.Cloneable, java.io.Serializable

  5. From JDK 1.2, Vector became part of collections framework. For example, instead of Enumeration interface, to iterate the elements, Vector can now use Iterator and ListIterator interfaces, use add() method to add the elements instead of addElement(), use get() to get the element instead of elementAt() etc.
  6. Coming to the coding part first import the java.util package. Vector supports four constructors. Knowledge of constructors is equally important to methods because a constructor gives the properties to an object at the time of object creation itself.
      Four Constructors of Vector

    1. Vector( ): Creates a Vector object with an initial capacity of 10 elements. The capacity is scaled up by 10 more elements whenever the existing capacity is exhausted with the elements added.
    2. Vector(int size): Creates a Vector object whose initial capacity is equal to size and increments by 10.
    3. Vector(int size, int increment): Creates a Vector object with an initial capacity of size and increments by increment when the existing capacity is filled up with elements.
    4. Vector(Collection c): Creates a Vector object with the elements of Collection class. Afterwards, elements can be added further as usual.
  7. Many methods like overloaded add(), overloaded addAll(), contains(), ensureCapacity(), containsAll(), elements(), firstElement(), lastElement(), elementAt(), indexOf(), insertElementAt() etc are available to be used in the coding.
In the following program on " What is Vector Java " many operations on Vector are performed.
import java.util.*;
public class VectorMethods
{
  public static void main(String args[])
  {
    Vector vect1 = new Vector();
    System.out.println("Before adding elements isEmpty(): " + vect1.isEmpty());
			// adding elements
    vect1.addElement("apple");
    vect1.add(10);
    vect1.addElement(10.5);
    Date d = new Date();
    vect1.add(d);
    vect1.add(10);
    StringBuffer sb1 = new StringBuffer("citrus");
    vect1.addElement(sb1);
                                             // inserting element
    vect1.insertElementAt("banana", 2);
                                            // miscellaneous methods
    System.out.println("After adding elements isEmpty(): " + vect1.isEmpty()); 
    System.out.println("Storing capacity of Vector: " + vect1.capacity());
    System.out.println("No. of elements in Vector: " + vect1.size());
    System.out.println("Vector contains 10.5: " +       vect1.contains(10.5));
    System.out.println("Index of 10: " + vect1.indexOf(10));
    System.out.println("Last index of 10: " + vect1.lastIndexOf(10));
                                              // retrieval of elements
    System.out.println("First element: " + vect1.firstElement()); 
    System.out.println("Last element: " + vect1.lastElement());
    System.out.println("Element at 3rd position: " + vect1.elementAt(3));

    System.out.println();
 }
}


What is Vector Java
Output screen of VectorMethods.java of Java Vector Methods

All the methods used in the code are explained in "Vector Methods".

More elaboration and many more operations like the retrieval of elements with Enumeration, Iterator, ListIterator and enhanced for loop etc. are available in the following postings:

1. Vector Generics
2. Vector Retrieval
3. Vector Play With
4. Differences between Vector and ArrayList

Leave a Comment

Your email address will not be published.