Vectors in Java
Vector Class
We may make resizable arrays comparable to the ArrayList class using the Vector class, which implements the List interface. A vector is similar to a dynamic collection that can modify its size, and we can utilise all the List interface's methods in vectors.
Vectors and the ArrayList are comparable. However, there are two differences:
- The vector is coordinated.
- There are several legacy methods in Java Vector that are not a part of a collections framework.
The syntax for Vector class
public class Vector<E> extends Object<E> implements List<E>, Cloneable, Serializable
The syntax for the creation of vector
Vector<Type> vector = new Vector<>();
Example for vector creation
Vectorcreation.java
import java.io.*;
import java.util.*;
public class Vectorcreation
{
public static void main(String[] args) {
// Integer type vector is created
Vector<Integer> vector= new Vector<>();
// String type vector is created
Vector<String> vector1= new Vector<>();
}
}
Output

Constructors used in Vector class
- vector()
A vector of default size ten is created - vector(int initialCapacity)
An empty vector is created, and the size will be zero with specified capacity and increment. - vector(int initialCapacity, int capacityincrement)
An empty vector is created with specified capacity and increment value. - Vector( Collection<? extends E> c)
Created a vector that contains elements of c
Methods in Vectors
1. Add elements to the vector
- add()
It adds an element to the end of the given vector
- add(index, element)
It adds described element at the preferred position
- addAll(vector)
It adds a vector to a vector
Program for the addition of elements to vectors
Vectorsadd.java
import java.util.Vector;
import java.io.*;
class Vectorsadd {
public static void main(String[] args) {
// Integer vector with name numbers is defined
Vector<Integer> numbers= new Vector<>();
//Use the add() method to add numbers 10 and 20 to the vector
numbers.add(10);
numbers.add(20);
System.out.println("Vector after adding 10 and 20 is : " +numbers);
// Using index number
numbers.add(2,50);
System.out.println("Vector: " +numbers);
// Using addAll()
// adding one vector number to the vector primes
Vector<Integer> primes = new Vector<>();
primes.add(3);
primes.addAll(numbers);
System.out.println("New Vector: " + primes);
}
}
Output

2. Accessing elements in the vector
To access elements of the vector, we use predefined methods of vector class such as get(index) and iterator methods.
1.get(index)
It returns an element specified by the given index
Program for accessing elements of the vector using the get(index) method
AccessEle.java
import java.util.Vector;
class AccessEle {
public static void main(String[] args) {
Vector<String> cars= new Vector<>();
cars.add("BMW");
cars.add("Range Rover");
cars.add("Ford");
// Using get()
String element = cars.get(2);
System.out.println("Element at index 2: " + element);
}
}
Output

2.iterator()
It is used to access elements of vector sequentially
Program for accessing elements of the vector using the iterator method
AccessEle.java
import java.util.Vector;
import java.util.Iterator;
class AccessEle {
public static void main(String[] args) {
Vector<String> cars= new Vector<>();
cars.add("BMW");
cars.add("Range Rover");
cars.add("Ford");
// Using iterator()
Iterator<String> iterate = cars.iterator();
System.out.print("Vector: ");
while(iterate.hasNext()) {
System.out.print(iterate.next());
System.out.print(", ");
}
}
}
Output

3. Removing elements in the vector
To remove some aspects from the vector
1. remove(index)
We can delete a particular index element from the vector using this method.
Program for removing elements of the vector using the remove(index) method
Remove.java
import java.util.Vector;
import java.util.Iterator;
class Remove {
public static void main(String[] args) {
Vector<String> cars= new Vector<>();
cars.add("BMW");
cars.add("Range Rover");
cars.add("Ford");
// Using remove(index) to remove element at index 1
String element = cars.remove(1);
System.out.println("Removed Element: " + element);
System.out.println("New Vector: " + cars);
}}
Output

2. clear()
Using this method, we can clear all elements from a particular vector.
Program for removing elements of vector using clear(index) method
Clear.java
import java.util.Vector;
import java.util.Iterator;
class Clear{
public static void main(String[] args) {
Vector<String> cars= new Vector<>();
cars.add("BMW");
cars.add("Range Rover");
cars.add("Ford");
// Using clear()
cars.clear();
System.out.println("Vector after clear(): " + cars);
}
}
Output

Example program for vectors in Java
Example1.java
import java.util.*;
public class Example1 {
public static void main(String args[]) {
//Create an empty Vector
Vector<Integer> v = new Vector<>();
//Add elements in the vector
v.add(10);
v.add(20);
v.add(30);
v.add(40);
v.add(50);
//Display the vector elements
System.out.println("Values in vector: " +v);
//use the remove() method to delete the first occurrence of an element
System.out.println("Remove first occurrence of element 20: "+v.remove((Integer)20));
System.out.println("Values in vector: " +v);
//Remove an element at index 5
v.removeElementAt(5);
System.out.println("Vector element after removal: " +v);
// Hash code is printed
System.out.println("Hash code of this vector = "+v.hashCode());
// we will get element at the specified index
System.out.println("Element at index 1 is = "+v.get(1));
}
}
Output
