Vector is a legacy class in Java that implements a dynamic array. It is part of the java.util package and is synchronized by default, making it thread-safe.
A Vector can store objects and uses indexing like arrays. It is similar to ArrayList but slower due to synchronization.
// Creating and using a Vector in Java
import java.util.Vector;
public class VectorDemo {
public static void main(String[] args) {
Vector names = new Vector<>();
names.add("Java");
names.add("Python");
names.add("C++");
System.out.println(names);
}
}
The output will display all elements stored in the Vector in the order they were added.
[Java, Python, C++]
Visualizing how capacity doubles when the Vector gets full.
ArrayList over Vector in single-threaded programs