Vector in Java

Java Vectors are often preferred over arrays due to their dynamic nature, automatically expanding as new data is added. Similar to linked lists, Vector instances have the ability to grow and shrink dynamically, allowing for flexible manipulation of their size. This capability enables elements to be easily added or removed from the Vector after its creation, providing convenience and adaptability in managing collections of data.

//Create a Vector with default initial size Vector vcTr = new Vector();
//Create a Vector with an initial size Vector vcTr = new Vector(300);

Capacity of a Vector

Vectors and ArrayLists differ in their resizing strategies. By default, a Vector doubles the size of its underlying array when it needs to accommodate additional elements, whereas an ArrayList increases its array size by 50 percent. This disparity in resizing behavior can impact performance depending on how these classes are utilized. Vectors optimize storage management by maintaining a capacity and a capacity increment.

The capacity of a Vector is always at least as large as its current size, and typically larger, as the storage expands in increments equal to the capacity increment. To mitigate frequent reallocation, an application can increase the capacity of a Vector before inserting a large number of components, thereby minimizing incremental resizing operations.

Vector(int initialCapacity, int capacityIncrement);


The Vector class in Java is designed to be thread-safe, ensuring that only one thread can access and modify the Vector at a time. This guarantees that while one thread is performing operations on the Vector, no other thread can interfere with its state. In contrast, the ArrayList class is not synchronized, allowing multiple threads to work on it simultaneously. Consequently, using ArrayList may result in concurrent modification exceptions if multiple threads attempt to modify the list concurrently. Therefore, when thread-safety is not a requirement, it is generally recommended to use ArrayList instead of Vector to avoid unnecessary synchronization overhead.

The following Java program illustrates several of the methods supported by this Vector collection Framework:

import java.util.*; class TestClass { public static void main (String[] args) throws java.lang.Exception { //create new Vector Object Vector vcTr = new Vector(); System.out.println("Vector Size: " + vcTr.size()); // initial size is 2, increment is 2 vcTr = new Vector(2,2); System.out.println("Vector Size: " + vcTr.size()); vcTr.addElement("Sunday"); vcTr.addElement("Monday"); vcTr.addElement("Wednesday"); System.out.println("Vector Size: " + vcTr.size()); System.out.println("Vector Capacity: " + vcTr.capacity()); //you can see here the capacity doubled as 4 //using Iterator to see all elemnets in vector Iterator<String> itr = vcTr.iterator(); while(itr.hasNext()){ System.out.println(; } //adding element at specified index vcTr.add(2,"Tuesday"); //using Enumeration to list all elemnts in a Vector Enumeration<String> enm = vcTr.elements(); while(enm.hasMoreElements()){ System.out.println(enm.nextElement()); } //Display first element from Vector System.out.println(vcTr.firstElement()); //Display last element from vector System.out.println(vcTr.lastElement()); //Retrieve vector elements by index System.out.println(vcTr.get(2)); //Search an item in Vector if(vcTr.contains("Monday")) System.out.println("Item Found"); //Remove a single element from Vector vcTr.remove(1); System.out.println(vcTr); //Remove all elements from vector vcTr.clear(); //Check Vector is empty or not if(vcTr.isEmpty()) System.out.println("Vector Empty !!"); else System.out.println("Vector Contains: " + vcTr.size() + " elements !!"); } }


Vector class in Java provides a dynamic, resizable array-like data structure that automatically expands as new elements are added. It is thread-safe, allowing only one thread to access and modify the vector at a time. While the Vector offers synchronized operations, this synchronization can introduce performance overhead. Thus, if thread-safety is not a concern, the ArrayList class is often preferred due to its higher performance.