The Wayback Machine - https://web.archive.org/web/20240618020204/https://www.geeksforgeeks.org/vector-lastindexof-method-in-java/
Open In App

Vector lastIndexOf() Method in Java

Last Updated : 24 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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).



Previous Article
Next Article

Similar Reads

Java.util.LinkedList.indexOf(), lastIndexof() in Java
Linked list library also offers to depict the first and last index of element that has to be found using the indexOf() and lastIndexOf() functions respectively. They offer a variety as the direct access is not available in the conventionally made linked list, hence knowledge of it is useful. 1. indexOf(Object o) : This method returns the index of t
4 min read
LinkedList lastIndexOf() Method in Java with Examples
The lastIndexOf(Object element) method of LinkedList class present inside java.util package is used to check and find the occurrence of a particular element in the list. If the element is present in the list then the lastIndexOf() method returns the index of the last occurrence of the element otherwise it returns -1. This method is used to find the
2 min read
CopyOnWriteArrayList lastIndexOf() method in Java
The lastIndexOf(Object o) method of CopyOnWriteArrayList returns the last occurrence of the element passed in the list. It returns -1 if the element is not present in the list. Syntax: public int lastIndexOf(Object o) Parameters: The function accepts a parameter o whose last occurrence is to be returned. Return Value: The function returns the last
3 min read
AbstractList lastIndexOf() method in Java with Examples
The lastIndexOf() method of java.util.AbstractList class is used to return the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the highest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index. Syntax: public int last
2 min read
StringBuilder lastIndexOf() method in Java with Examples
In StringBuilder class, there are two types of lastIndexOf() method depending upon the parameters passed to it. lastIndexOf(String str) The lastIndexOf(String str) method of StringBuilder class is the inbuilt method used to return the index within the String for last occurrence of passed substring as parameter. The last occurrence of the empty stri
4 min read
StringBuffer lastIndexOf() method in Java with Examples
In StringBuffer class, there are two types of lastIndexOf() method depending upon the parameters passed to it. lastIndexOf(String str) The lastIndexOf(String str) method of StringBuffer class is the inbuilt method used to return the index within the String for last occurrence of passed substring as parameter. The last occurrence of the empty string
4 min read
Stack lastIndexOf() method in Java with Example
The Java.util.Stack.lastIndexOf(Object element) method is used to check and find the occurrence of a particular element in the Stack. If the element is present in the Stack 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 elemen
2 min read
AbstractSequentialList lastIndexOf() method in Java with Example
The lastIndexOf() method of java.util.AbstractSequentialList class is used to return the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that (o==null ? get(i)==null : o.equals(get(i))), or -1 if there is no such index. Syntax: publi
2 min read
Stack lastIndexOf(Object, int) method in Java with Example
The Java.util.Stack.lastIndexOf(Object element, int last_index) method is used to the last index of the first occurrence of the specified element in this Stack, searching forwards from last index, or returns -1 if the element is not found. More formally, returns the lowest last index i such that (i &gt;= last index &amp;&amp; Objects.equals(o, get(
3 min read
List lastIndexOf() Method in Java with Examples
This method returns the last index of the occurrence of the specified element in this list, or -1 if this list does not contain the element. Syntax: int lastIndexOf(Object o) Parameters: This function has a single parameter, i.e, the element to be searched in the list. Returns: This method returns the last index of occurrence of the given element i
2 min read