Vectors in Java are one of the most commonly used data structures in the programming world. We all know that Arrays are data structures that hold the data in a linear fashion. Vectors also store the data in a linear fashion, but unlike Arrays, they do not have a fixed size. Instead, their size can be increased on demand.
Vector class is a child class of AbstractList class and implements on List interface. To use Vectors, we first have to import Vector class from java.util package: import java.util.Vector
Vectors Constructors
Listed below are the multiple variations of vector constructors available to use:
- Vector(int initialCapacity, int Increment)-Constructs a vector with given initialCapacity and its Increment in size.
- Vector(int initialCapacity)-Constructs an empty vector with given initialCapacity. In this case, Increment is zero.
- Vector()-Constructs a default vector of capacity 10.
- Vector(Collection c)-Constructs a vector with a given collection, the order of the elements is same as returned by the collection’s iterator.
There are also three protected parameters in vectors
- Int capacityIncrement()- It automatically increases the capacity of the vector when the size becomes greater than capacity.
- Int elementCount()-tell number of elements in the vector
- Object[] elementData()-array in which elements of vector are stored
Advantages of Vector in Java
- The property of having a dynamic size is very much useful as it avoids the memory wastage in case we do not know the size of the data-Structures at the time of declaration.
- When we want to change the size of our data structure in the middle of a program, vectors can prove to be very useful.
The property of having a dynamic size is not unique to Vectors in Java. Another data structure, known as ArrayList also shows the property of having a dynamic size. However, Vectors are different from ArrayLists due to a couple of reasons:
- First, Vectors are synchronized which gives it an advantage over ArrayList as compared to multi-threaded programs as there are risks of data corruption.
- Secondly, Vectors have some legacy functions which can be implemented only on vectors and not on ArrayLists.
Example: Initializing Vector Constructors
// Java code illustrating Vector Constructors
import
java.util.*;
public
class
Main{
public
static
void
main(String[] args)
{
// create default vector
Vector v1 = new
Vector();
// create a vector of given Size
Vector v2 = new
Vector(20);
// create a vector of given Size and Increment
Vector v3 = new
Vector(30,10);
v2.add(100);
v2.add(100);
v2.add(100);
// create a vector with given collection
Vector v4 = new
Vector(v2);
System.out.println("Vector v1 of capacity "
+ v1.capacity());
System.out.println("Vector v2 of capacity "
+ v2.capacity());
System.out.println("Vector v3 of capacity "
+ v3.capacity());
System.out.println("Vector v4 of capacity "
+ v4.capacity());
}
Methods in Vector
Let’s have a look at few very frequently used vector methods.
- Boolean add(Object o) — It appends an element at the end of the vector.
- Void add (int Index, E element) — It adds the given element at the specified index in the vector.
- Boolean Remove(object o) — It removes remove the element at the given index in the vector.
- Boolean removeElement(Object obj) — It deletes the element by its name obj (not by index number).
- Int size() — It returns the size of the vector.
- Int Capacity() — It returns the capacity of the vector.
- Object get(int index) — It returns the element at the given position in the vector.
- Object firstElement() — It returns the first element.
- Object lastElement() — It returns the last element.
- Boolean equals(Object o) — It compares the vector with the specified object for equality. It returns true if all elements are true at their corresponding places.
- Void trimtosize() — This method removes extra capacity and keeps the capacity just to hold the elements i.e. equal to the size.