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.

Vector

Although vector is similar to ArrayList in many ways, there exist some differences too. The Vector class is synchronized and several legacy methods are contained by 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 reengineered Vectors and made them fully compatible with Collections.

We use the following syntax to create a Vector.

Syntax

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.

Program to Search elements in Vector by its Index

indexOf()

To search for an element in Vector by its index we can use this method. There exist two ways of using the ‘indexOf()’ method −

  • 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

In the following example, we will define a vector named ‘vectlist’ and store a few objects in it by using the ‘add()’ method. Then, using 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

The following example demonstrates 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

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 the usefulness of the indexOf() method while searching for a particular element in a Vector. We also learned about Vector in Java.

Updated on: 15-May-2023

63 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements