Last Updated : 11 Jul, 2025
The Java.util.Vector.lastIndexOf(Object element) method is used to check and find the occurrence of a particular element in the vector. If the element is present in the vector then the lastIndexOf() method returns the index of last occurrence of the element otherwise it returns -1. This method is used to find the last occurrence of a particular element in a vector.
Syntax:
Vector.lastIndexOf(Object element)
Parameters: The parameter element is of type Vector. It refers to the element whose last occurrence is required to be checked.
Return Value: The method returns the position of the last occurrence of the element in the Vector. If the element is not present in the Vector then the method returns -1. The returned value is of integer type.
Below programs illustrate the Java.util.Vector.lastIndexOf() method:
Program 1:
Java
// Java code to illustrate lastIndexOf()
import java.util.*;
public class VectorDemo {
public static void main(String args[])
{
// Creating an empty Vector
Vector<String> vec_tor = new Vector<String>();
// Use add() method to add elements in the Vector
vec_tor.add("Geeks");
vec_tor.add("for");
vec_tor.add("Geeks");
vec_tor.add("10");
vec_tor.add("20");
// Displaying the Vector
System.out.println("Vector: " + vec_tor);
// The last position of an element is returned
System.out.println("Last occurrence of Geeks is at index: "
+ vec_tor.lastIndexOf("Geeks"));
System.out.println("Last occurrence of 10 is at index: "
+ vec_tor.lastIndexOf("10"));
}
}
Output:
Vector: [Geeks, for, Geeks, 10, 20] Last occurrence of Geeks is at index: 2 Last occurrence of 10 is at index: 3
Program 2:
Java
// Java code to illustrate lastIndexOf()
import java.util.*;
public class VectorDemo {
public static void main(String args[])
{
// Creating an empty Vector
Vector<Integer> vec_tor = new Vector<Integer>();
// Use add() method to add elements in the Vector
vec_tor.add(10);
vec_tor.add(22);
vec_tor.add(3);
vec_tor.add(10);
vec_tor.add(20);
// Displaying the Vector
System.out.println("Vector: " + vec_tor);
// The last position of an element is returned
System.out.println("Last occurrence of 10 is at index: "
+ vec_tor.lastIndexOf(10));
System.out.println("Last occurrence of 20 is at index: "
+ vec_tor.lastIndexOf(20));
}
}
Output:
Vector: [10, 22, 3, 10, 20] Last occurrence of 10 is at index: 3 Last occurrence of 20 is at index: 4
Time complexity: O(n). // n is the size of the vector.
Auxiliary Space: O(n).
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4