Java Vector indexOf() lastIndexOf() Methods Tutorial

In this section, we will learn what the Vector indexOf() and lastIndexOf() methods are and how to use them in Java.

What is Java Vector indexOf() Method?

The Java Vector indexOf() method is used to find the index number of an element.

Basically, this method takes one argument and that is the value we want to search for its index number in a vector object.

This method will then search for that argument and it will return the index number of the first occurrence of the value in the target vector object.

Java Vector indexOf() Method Syntax:

public int indexOf(Object o)

public int indexOf(Object o, int index)

indexOf() Method Parameters:

The method has two variants:

  • In the first variant, we only need to set the element we’re looking for in the target vector.
  • Bun in the second variant, we can set a second argument and that is the index number from which we want this search to start from.

Note: the default index is 0.

indexOf() Method Return Value:

The return value of this method is the index number of the first occurrence of the value we’ve set as the argument of the method.

Note: if the value wasn’t in the target vector object, the return value becomes -1.

indexOf() Method Exceptions:

IndexOutOfBoundsException: We get this exception if we set an index number as the second argument of this method that is out of the bound of the indexes of the target vector object.

Example: using Vector indexOf() method

import java.util.Vector; 

public class Main {

    public static void main(String[] args){
        
      Vector <Integer> vector = new Vector<>();
      vector.addElement(1);
      vector.addElement(2);
      vector.addElement(3);
      vector.addElement(4);
      vector.addElement(5);
      
      System.out.println(vector.indexOf(2));
    }
}

Output:

1

What is Java Vector lastIndexOf() Method?

The Java Vector lastIndexOf() method is used to return the index number of the last occurrence of an element that we set as the argument of this method.

Basically, this method searches for an element from the last index of a vector object toward its starting position. It’s the opposite of the indexOf() method.

Java Vector lastIndexOf() Method Syntax:

public int lastIndexOf(Object o)

public int lastIndexOf(Object o, int index)

lastIndexOf() Method Parameters:

The method has two variants:

The first variant only takes one argument and that is the element we want to get the index number of its last occurrence in a vector object.

For the other variant of this method, it also takes a second argument and that is the index number we want the search to start from. For example, if you set the value 10, that means start the search for the target element from the index number 10 and move toward the beginning (index 0) of that vector object.

lastIndexOf() Method Return Value:

The return value of this method is the index number of the last occurrence of the value we’ve set as the argument of this method.

Note: if the method can’t find the element in the target vector object, the return value of this method becomes -1.

lastIndexOf() Method Exceptions:

This method also might throw the same exception as the indexOf() method.

Example: using Vector lastIndexOf() method

import java.util.Vector; 

public class Main {

    public static void main(String[] args){
        
      Vector <Integer> vector = new Vector<>();
      vector.addElement(1);
      vector.addElement(2);
      vector.addElement(3);
      vector.add(5);
      vector.addElement(4);
      vector.addElement(5);
      
      System.out.println(vector.lastIndexOf(5));
    }
}

Output:

5

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies