Vector Class in Java

The Vector class in Java is a collection class that is used to implement dynamic arrays. It is similar to the ArrayList class in Java, but it is synchronized, which means that it can be accessed by multiple threads at the same time without any issues. In this article, we will discuss the Vector class in Java in detail.

What is Vector Class in Java?


The Vector class in Java is a part of the java.util package, and it is used to implement dynamic arrays. A dynamic array is an array that can grow or shrink in size at runtime. The Vector class is similar to an ArrayList class, but it is synchronized, which means that it is thread-safe.

Vector Syntax:


The syntax for creating a Vector in Java is as follows:

Vector vectorName = new Vector();


In this syntax, “DataType” refers to the data type of the elements that will be stored in the Vector, and “vectorName” is the name of the Vector.

Vector Methods:


The Vector class provides a set of methods that can be used to manipulate the elements in the Vector. Here are some of the most commonly used methods:

  1. add(element): This method is used to add an element to the end of the Vector.
  2. add(index, element): This method is used to add an element at a specific index in the Vector.
  3. remove(index): This method is used to remove an element from the Vector at a specific index.
  4. get(index): This method is used to retrieve the element at a specific index in the Vector.
  5. set(index, element): This method is used to replace an element at a specific index in the Vector with a new element.
  6. size(): This method is used to get the number of elements in the Vector.
  7. capacity(): This method is used to get the current capacity of the Vector.
  8. trimToSize(): This method is used to trim the capacity of the Vector to the current size of the Vector.
  9. ensureCapacity(minCapacity): This method is used to ensure that the Vector has a minimum capacity of minCapacity.


Example of Vector in Java:


Here is an example of how to use a Vector in Java:

import java.util.Vector;
public class VectorExample {
public static void main(String[] args) {
Vector vector = new Vector();
vector.add("apple");
vector.add("banana");
vector.add("orange");
vector.add(1, "grape");
vector.remove(2);
System.out.println(vector.get(1));
vector.set(0, "pear");
System.out.println(vector.size());
System.out.println(vector.capacity());
vector.ensureCapacity(6);
vector.trimToSize();
}
}


In this example, we first import the Vector class from the java.util package. We then create a new Vector called “vector” that can hold strings. We add four elements to the Vector, with “grape” being added at index 1. We then remove the element at index 2, which is “orange”. We print out the element at index 1, which is “grape”. We then replace the element at index 0, which is “apple”, with “pear”. Finally, we print out the size of the Vector, which is 3, the capacity of the Vector, which is 10 by default, and then we ensure that the Vector has a minimum capacity of 6, and trim the capacity of the Vector to the current size.

Conclusion:


In conclusion, the Vector class in Java is a useful data structure that is used to implement dynamic arrays. It is similar to the ArrayList class, but it is synchronized, which means that it is thread-safe.