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 "));     } } |
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 ));     } } |
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).