Java Vector lastIndexOf(Object o) Example

The indexOf(Object o) method of java.util.Vector class will get you the index of the first occurrence of the specified element in the vector whereas lastIndexOf(Object o) method will get the index of the last occurrence of the specified element in the vector

Signature

public int lastIndexOf(Object o) 

This method returns the index of the last occurrence of the specified element in the vector, or -1 if the vector does not contain the specified element

Example

The following example shows the usage of java.util.Vector.lastIndexOf(o) method.

import java.util.Vector;

public class LastIndexOfMethodVectorExample 
{
    public static void main(String args[])
    {
     // create an empty Vector
        Vector vector1 = new Vector();
        
        // use add() method to add elements to the Vector
        vector1.add("Element1");
        vector1.add("Element2");
        vector1.add("Element1");
        vector1.add("Element3");
        vector1.add("Element1");
        vector1.add("Element4");
        vector1.add("Element1");
        
        //Lets get the last index of Element1
        System.out.println("Last Index of \"Element1\" is : "+vector1.lastIndexOf("Element1"));
    }
}

Output

Last Index of "Element1" is : 6

Leave a Reply

Your email address will not be published. Required fields are marked *