Vector

A Vector in Java is a legacy class that belongs to the java.util package and is similar to an ArrayList. It represents a dynamic array that can grow or shrink in size as elements are added or removed. Vector is synchronized, meaning it is thread-safe, which ensures that multiple threads can safely access and modify a Vector instance concurrently without causing data corruption.

import java.util.Vector;
// Syntax for creating a Vector
Vector<Type> vectorName = new Vector<>();Code language: JavaScript (javascript)

Methods:

  1. Adding Elements
    • add(element): Appends the specified element to the end of the vector.
    • add(index, element): Inserts the specified element at the specified position in the vector.
  2. Accessing Elements
    • get(index): Returns the element at the specified index in the vector.
    • firstElement(): Returns the first element of the vector.
    • lastElement(): Returns the last element of the vector.
  3. Removing Elements
    • remove(index): Removes the element at the specified index from the vector.
    • removeElement(element): Removes the first occurrence of the specified element from the vector.
    • removeAllElements(): Removes all elements from the vector.
  4. Size and Capacity
    • size(): Returns the number of elements currently in the vector.
    • capacity(): Returns the current capacity of the vector.
  5. Other Operations
    • contains(element): Checks if the vector contains the specified element.
    • clear(): Removes all elements from the vector.
    • toArray(): Converts the vector to an array.
import java.util.Vector;
public class StudentVectorExample {
    public static void main(String[] args) {
        // Creating a Vector to store student names
        Vector<String> studentVector = new Vector<>();

        // Adding students to the Vector
        studentVector.add("Paani");
        studentVector.add("Mahesh");
        studentVector.add("Datta");
        studentVector.add("Ganesh");
        studentVector.add("Harsha");

        // Displaying the names of students
        System.out.println("List of students:");
        for (String student : studentVector) {
            System.out.println(student);
        }

        // Adding a new student at index 2
        studentVector.add(2, "NewStudent");

        // Displaying the updated list
        System.out.println("\nUpdated list of students:");
        for (String student : studentVector) {
            System.out.println(student);
        }

        // Removing a student at index 3
        studentVector.remove(3);

        // Displaying the final list
        System.out.println("\nFinal list of students:");
        for (String student : studentVector) {
            System.out.println(student);
        }
    }
}

D:\>javac StudentVectorExample.java

D:\>java StudentVectorExample
List of students:
Paani
Mahesh
Datta
Ganesh
Harsha

Updated list of students:
Paani
Mahesh
NewStudent
Datta
Ganesh
Harsha

Final list of students:
Paani
Mahesh
NewStudent
Ganesh
HarshaCode language: PHP (php)

Key Features of Vector

  • Thread-Safety: Vector is synchronized, making it suitable for concurrent access from multiple threads without external synchronization.
  • Dynamic Resizing: Like ArrayList, Vector can grow dynamically as elements are added, and it can shrink as elements are removed.
  • Performance: Due to its synchronized nature, Vector operations can be slower compared to ArrayList when not accessed concurrently.
  • Legacy Considerations: While Vector provides thread-safety, its synchronization overhead can be a disadvantage in performance-sensitive applications compared to ArrayList or other modern collections.

In Java programming, Vector offers a synchronized alternative to ArrayList for situations where thread safety is required. It provides flexibility in managing collections of elements with methods for adding, accessing, and removing elements efficiently. However, careful consideration of its synchronization overhead is necessary when choosing between Vector and other collection types based on specific application requirements.

Scroll to Top