Java Utility Library

Java Vector - lastIndexOf() Method



The java.util.Vector.lastIndexOf() method returns index of the last occurrence of the specified element in the vector. It returns -1 if element is not present in the vector.

Syntax

public int lastIndexOf(Object obj)

Parameters

obj Specify the element to search for in the vector.

Return Value

Returns the index of the last occurrence of the specified element in the vector, or -1 if element is not present in the vector.

Exception

NA.

Example:

In the example below, the java.util.Vector.lastIndexOf() method is used to find the index of last occurrence of the specified element in the vector MyVector.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a vector
    Vector<Integer> MyVector = new Vector<Integer>();

    //populating vector
    MyVector.add(10);
    MyVector.add(30);
    MyVector.add(20);
    MyVector.add(50);
    MyVector.add(30);

    System.out.print("30 appeared last time at index=" + MyVector.lastIndexOf(30)); 
  }
}

The output of the above code will be:

30 appeared last time at index=4

❮ Java.util - Vector