Curriculum
Vector is a class in Java that provides a dynamic array that is similar to ArrayList, but with built-in synchronization. It allows for easy addition, removal, and retrieval of elements, while ensuring thread safety. In this answer, I will provide an explanation of Vector in Java with examples of how to create and use it, as well as some commonly used methods.
To create a Vector, you must first import the java.util.Vector class. You can create a Vector in Java using one of the following methods:
// Method 1: create an empty Vector and add elements to it later
Vector<String> vector1 = new Vector<>();
// Method 2: create a Vector with initial capacity
Vector<String> vector2 = new Vector<>(10);
// Method 3: create a Vector with initial capacity and capacity increment
Vector<String> vector3 = new Vector<>(10, 5);
// Method 4: create a Vector with initial elements
Vector<String> vector4 = new Vector<>(Arrays.asList("apple", "banana", "cherry"));
In the above example, we create four different Vectors. vector1 is an empty Vector that can be used to add elements later. vector2 is a Vector with an initial capacity of 10. This means that it will be able to hold up to 10 elements without resizing. vector3 is a Vector that is initialized with an initial capacity of 10 and a capacity increment of 5. This means that the capacity of the Vector will increase by 5 every time it needs to be resized. vector4 is a Vector that is initialized with three elements.
Once a Vector has been created, elements can be added or removed from it using the following methods:
// Add an element to the end of the Vector
vector.add("orange");
// Add an element at a specific index
vector.add(2, "pear");
// Remove an element at a specific index
vector.remove(1);
// Remove the first occurrence of a specific element
vector.remove("apple");
// Remove all elements from the Vector
vector.clear();
Vector provides several methods to access elements in the list:
// Get the element at a specific index
String element = vector.get(0);
// Set the element at a specific index
vector.set(1, "pear");
// Get the index of the first occurrence of an element
int index = vector.indexOf("banana");
// Get the index of the last occurrence of an element
index = vector.lastIndexOf("banana");
// Check if the Vector contains an element
boolean contains = vector.contains("banana");
Some other commonly used methods provided by the Vector class include:
// Get the size of the Vector
int size = vector.size();
// Check if the Vector is empty
boolean isEmpty = vector.isEmpty();
// Convert the Vector to an array
String[] array = vector.toArray(new String[0]);
// Sort the elements in the Vector
Collections.sort(vector);
// Iterate over the elements in the Vector using a for-each loop
for (String element : vector) {
System.out.println(element);
}
One thing to keep in mind when using Vector is that it is synchronized. While this ensures that the Vector is thread-safe, it can also result in decreased performance when multiple threads are accessing the same Vector instance. If you do not need synchronization, you can use the ArrayList class instead.