Vector lastIndexOf() Method in Java

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Output:

Vector: [10, 22, 3, 10, 20]
Last occurrence of 10 is at index: 3
Last occurrence of 20 is at index: 4


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.