Java Vector lastIndexOf(Object o, int index) Method Example

In our previous example we have learnt about lastIndexOf(Object o) method of java.util.Vector class will get the index of the last occurrence of the specified element in the vector whereas lastIndexOf(Object o, int index) method will return the index of the last occurrence of the specified element in the vector, searching backwards from index, or returns -1 if the element is not found.

Signature

public int lastIndexOf(Object o,int index) 

This method will return the index of the last occurrence of the specified element in the vector, searching backwards from index, or returns -1 if the element is not found.

Example

The following example shows the usage of java.util.Vector.lastIndexOf(o,index) 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",5));
    }
}

Output

Last Index of "Element1" is : 4

Leave a Reply

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