Java Vector Class
Java Vector Class is a part of the Java Collections Framework. It implements a growable array of objects. Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains components that can be accessed using an index. However, the size of a vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.
What is Vector in Java?
In Java, a Vector class is like the dynamic array which can grow or shrink its size. It is similar to ArrayList, but with two differences – Vector is synchronized, and it contains many legacy methods that are not the part of collections framework.
Vector proves to be very useful if you don't know the size of the array in advance, or you just need one that can change sizes over the lifetime of a program.
Declaration of Vector in Java
In Java, Vector can be declared as:
Vector vec = new Vector(); // Default constructor (creates an empty vector)
Vector vec = new Vector(int size); // Creates an empty vector with the specified initial capacity
Vector vec = new Vector(int size, int increment); // Creates an empty vector with the specified initial capacity and capacity increment
Vector vec = new Vector(Collection c); // Creates a vector containing the elements of the specified collection
Methods of Vector Class in Java
Java Vector class includes various methods. Here are some of the most commonly used:
- void add(int index, E element): This method is used to insert an element at the specified position in this Vector.
- boolean add(E e): This method is used to append the specified element to the end of this Vector.
- boolean addAll(Collection<? extends E> c): This method appends all of the elements in the specified Collection to the end of this Vector.
- void clear(): This method is used to remove all of the elements from this Vector.
- boolean contains(Object o): This method returns true if this Vector contains the specified element.
Example of Java Vector Class
Here is an example of how a Vector is used in Java:
import java.util.*;
public class Main{
public static void main(String args[]){
Vector<String> vec = new Vector<String>(2);
/*Adding elements to a vector*/
vec.addElement("Apple");
vec.addElement("Orange");
vec.addElement("Mango");
vec.addElement("Fig");
/* check size and capacityIncrement*/
System.out.println("Size is: "+vec.size());
System.out.println("Default capacity increment is: "+vec.capacity());
vec.addElement("fruit1");
vec.addElement("fruit2");
vec.addElement("fruit3");
/*size and capacityIncrement after two insertions*/
System.out.println("Size after addition: "+vec.size());
System.out.println("Capacity after increment is: "+vec.capacity());
/*Display Vector elements*/
Enumeration en = vec.elements();
System.out.println("\nElements are:");
while(en.hasMoreElements())
System.out.print(en.nextElement() + " ");
}
}
In the above example, we have a vector of capacity 2. We add four elements to it, and the capacity of the vector grows automatically to accommodate the elements.
Conclusion
Java Vector class is useful when you're dealing with a collection of objects and you need a dynamic data structure that can grow or shrink during the course of your program. It's synchronized, which means it's safe to use in multi-threaded applications, and it also contains a number of useful methods for manipulating the elements within the Vector.