Searching Elements in Vector Using Index in Java



Vectors implement the List interface and are used to create dynamic arrays. The array whose size is not fixed and can grow as per our needs is called as a dynamic array. The vectors are very similar to ArrayList in terms of use and features.

In this article, we will learn how we can create a vector and search for a particular element by its index in Java. Let's discuss Vector first.

Java Vector

Vector is similar to ArrayList in many ways but there exist some differences too. The Vector class is synchronized and several legacy methods are contained in it.

Synchronized: Whenever we perform an operation on vectors it restricts its access to multiple threads at same time. If we try to access a vector by two or more threads at the same time then, it will throw an exception named ConcurrentModificationException. This makes it less efficient as compared to ArrayList.

Legacy Class: Before the release of Java version 1.2 when the Collection Framework was not introduced, there were some classes that depicts the features of this framework's classes and were used in the place of those classes. For example, Vector, Dictionary, and Stack. With JDK 5, Java creators re-engineered Vectors and made them fully compatible with Collections.

Syntax

We use the following syntax to create a Vector:

Vector<TypeOfCollection> nameOfCollection = new Vector<>();

Here, in TypeOfCollection specify the data type of the elements that will be stored in collection. In nameOfCollection give suitable to your collection.

How to Search elements in Vector by its Index?

To search for an element in Vector by its index, we can use indexOf() method. There are two ways of using this method, which are:

  • indexOf(nameOfObject): It takes an object as an argument and returns its integer value of index. If the object does not belong to the specified collection, it simply returns -1.

  • indexOf(nameOfObject, index): It takes two arguments one is an object and the other is an index. It will start searching for the object from specified index value.

Example 1: Using indexOf() with Single Argument

In the following example, we will define a vector and using the indexOf() method with single argument, we will search for the element.

import java.util.*;
public class VectClass {
   public static void main(String args[]) {
      // Creating a vector 
      Vector< String > vectList = new Vector<>();
      // Adding elements in the vector
      vectList.add("Tutorix");
      vectList.add("Simply");
      vectList.add("Easy");
      vectList.add("Learning");
      vectList.add("Tutorials");
      vectList.add("Point");
      // storing value of index in variable
      int indexValue = vectList.indexOf("Tutorials");
      System.out.println("Index of the specified element in list: " + indexValue);
   }
}

Output

Index of the specified element in list: 4

Example 2: Searched Element not Available

The following example demonstrates that if the element is not available in the collection then 'indexOf()' returns -1.

import java.util.*;
public class VectClass {
   public static void main(String args[]) {
      // Creating a vector 
      Vector< String > vectList = new Vector<>();
      // Adding elements in the vector
      vectList.add("Tutorix");
      vectList.add("Simply");
      vectList.add("Easy");
      vectList.add("Learning");
      vectList.add("Tutorials");
      vectList.add("Point");
      // storing value of index in variable
      int indexValue = vectList.indexOf("Tutorialspoint");
      System.out.println("Index of the specified element in list: " + indexValue);
   }
}

Output

Index of the specified element in list: -1

Example 3: Using indexOf() with Two Arguments

The following example illustrates the use of 'indexOf()' with two arguments. The compiler will start searching for the given element from index 3.

import java.util.*;
public class VectClass {
   public static void main(String args[]) {
      // Creating a vector 
      Vector< String > vectList = new Vector<>();
      // Adding elements in the vector
      vectList.add("Tutorix");
      vectList.add("Simply");
      vectList.add("Easy");
      vectList.add("Learning");
      vectList.add("Tutorials");
      vectList.add("Point");
      vectList.add("Easy");
      vectList.add("Learning");
      // storing value of index in variable
      int indexValue = vectList.indexOf("Easy", 3);
      System.out.println("Index of the specified element in list: " + indexValue);
   }
}

Output

Index of the specified element in list: 6

Conclusion

In this article, we have discussed a few examples that show how to use the indexOf() method to search for a particular element in a Vector. We also learned about Vector in Java.

Updated on: 2025-09-01T12:13:21+05:30

212 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements