A Vector
in Java is a part of the java.util
package and is a class that implements a growable array of objects. It works similarly to an ArrayList
but with a few key differences. The main characteristic of a Vector
is that it can grow or shrink dynamically as elements are added or removed.
Unlike traditional arrays, which have a fixed size, a Vector
automatically expands when it runs out of space. The vector size doubles when the elements exceed its current capacity, making it highly flexible when dealing with an unknown or variable number of elements.
Features
- Dynamic Sizing: As more elements are added, the size of a
Vector
automatically increases. - Thread-Safety: Vectors are synchronized by default, meaning multiple threads can access a
Vector
concurrently without running into problems. However, this can makeVector
slower thanArrayList
in single-threaded environments. - Growth Policy: When the vector exceeds its current size, its capacity is automatically increased. By default, this capacity doubles, but this can be adjusted when creating the vector.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import java.util.Vector; public class VectorOperationsExample { public static void main(String[] args) { // Create a vector and add elements Vector<String> colors = new Vector<>(); colors.add("Red"); colors.add("Blue"); colors.add("Green"); System.out.println("Initial Vector: " + colors); // Adding an element at a specific index colors.add(1, "Yellow"); // Insert "Yellow" at index 1 System.out.println("After adding 'Yellow' at index 1: " + colors); // Accessing elements String colorAtIndex2 = colors.get(2); // Retrieves the element at index 2 (Green) System.out.println("Element at index 2: " + colorAtIndex2); // Checking if vector contains an element boolean containsBlue = colors.contains("Blue"); System.out.println("Contains 'Blue': " + containsBlue); // Removing an element by object colors.remove("Red"); // Removes "Red" from the vector System.out.println("After removing 'Red': " + colors); // Removing an element by index colors.remove(1); // Removes element at index 1 (Yellow) System.out.println("After removing element at index 1: " + colors); // Getting size and capacity System.out.println("Current size of vector: " + colors.size()); System.out.println("Current capacity of vector: " + colors.capacity()); // Clearing all elements colors.clear(); System.out.println("After clearing the vector: " + colors); // Checking if the vector is empty boolean isEmpty = colors.isEmpty(); System.out.println("Is the vector empty? " + isEmpty); // Re-populating the vector colors.add("Purple"); colors.add("Orange"); // Getting the index of an element int indexOfPurple = colors.indexOf("Purple"); System.out.println("Index of 'Purple': " + indexOfPurple); // Trimming to size (reduce capacity to current size) colors.trimToSize(); System.out.println("After trimming to size: " + colors); } } |
Use Cases
- Dynamic data storage: Ideal when the size of the data is not known in advance, and the collection may grow or shrink.
- Thread-safe collections: Useful in multi-threaded applications where thread safety is a concern.
- Data manipulation: When there is a need to perform frequent insertions, deletions, or random access to elements.