Java Vector

Methods of Vector

Java Vector is a dynamic data structure that can resize itself automatically like an array making efficient in managing collections of objects

It is similar to an ArrayList but is synchronized, which means that it is thread-safe and can be used in multi-threaded environments.

Java Vector also have some additional methods for inserting and removing elements at specific positions, as well as checking if an element is present in the Vector.

What is Java Vector Class?

The Java Vector class is a legacy data structure in Java that provides a dynamic array-like implementation of the List interface. It was introduced in Java 1.0 and is thread-safe, meaning that it can be safely accessed by multiple threads without explicit synchronization.

The Vector class implements several interfaces, including List, Collection, Iterable, and Enumeration. Some of its commonly used methods include add, remove, get, set, indexOf, lastIndexOf, and size.

In Java, you can initialize a Vector object using several different approaches, including:

  • Default Constructor: You can create an empty Vector object using the default constructor, as follows:
Vector<Object> vector = new Vector<>();
  • Constructor with Initial Capacity: You can create a Vector object with a specific initial capacity using the following constructor : where initialCapacity is an integer value that represents the initial size of the Vector.
Vector <object>vector = new Vector<>(initialCapacity);
  • Constructor with Capacity Increment: You can also create a Vector object with a specific capacity increment using the following constructor:
  • where initialCapacity is an integer value that represents the initial size of the Vector.
  • CapacityIncrement is an integer value that specifies the amount by which the Vector’s capacity should be increased when it needs to grow beyond its current capacity.
Vector <object> vector = new Vector<>(initialCapacity, capacityIncrement);
  • Initialization with Elements: You can also create a Vector object and initialize it with a collection of elements using the following constructor:
Vector <Object> vector = new Vector<>(initialCapacity);

where element1, element2, …, elementN are the elements that you want to add to the Vector.

  • Note that in each case, you can replace the Object type parameter with the type of the objects that you want to store in the Vector.

Java Vector vs ArrayList 

Java Vector vs ArrayList

Vector Methods In Java 

MethodsDescription
add(element): Adds an element to the end of the Vector.
add(index, element): Inserts an element at the specified index in the Vector.
addAll(collection): Adds all elements of a collection to the end of the Vector.
get(index):Returns the element at the specified index in the Vector.
set(index, element):Replaces the element at the specified index in the Vector with the specified element.
remove(element): Removes the first occurrence of the specified element from the Vector.
size(): Returns the number of elements in the Vector.
remove(index): Removes the element at the specified index in the Vector.

The Java Vector class also provides several other methods for manipulating and accessing the elements of the Vector.

Let’s look at the Java Vector to perform certain operations.

Example 1: Java Program to Show Java vector

Run

import java.util.Vector;

 
public class Main
{
  
public static void main (String[]args)
  {
    
      // Create a new Vector of integers
      Vector < Integer > numbers = new Vector < Integer > ();
    
 
      // Add some elements to the Vector
      numbers.add (1);
    
numbers.add (2);
    
numbers.add (3);
    
 
      // Print the contents of the Vector
      System.out.println ("Vector: " + numbers);
    
 
      // Remove an element from the Vector
      numbers.remove (1);
    
 
      // Print the contents of the Vector again
      System.out.println ("Vector after removing element: " + numbers);
    
 
      // Check if an element is present in the Vector
      if (numbers.contains (3))
      {
	
System.out.println ("Vector contains the element 3");
      
}
    else
      {
	
System.out.println ("Vector does not contain the element 3");
      
}
  
}

}

Output

Vector: [1, 2, 3]
Vector after removing element: [1, 3]
Vector contains the element 3

Example 2: Java Program to Create a Vector 

Run

import java.util.Vector;

 
public class Main
{
  
public static void main (String[]args)
  {
    
      // Create an empty Vector of Strings
      Vector < String > vector = new Vector < String > ();
    
 
      // Add some elements to the Vector
      vector.add ("IPL");
    
vector.add ("PPL");
    
vector.add ("CPL");
    
 
      // Print the contents of the Vector
      System.out.println ("Vector: " + vector);
    
 
      // Create a Vector with initial capacity of 5
      Vector < Integer > numbers = new Vector < Integer > (5);
    
 
      // Add some elements to the Vector
      numbers.add (1);
    
numbers.add (2);
    
numbers.add (3);
    
 
      // Print the contents of the Vector
      System.out.println ("Numbers Vector: " + numbers);

} 
} 

Output

Vector: [IPL, PPL, CPL]
Numbers Vector: [1, 2, 3]

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription