Javatpoint Logo
Javatpoint Logo

Java Vector lastIndexOf() Method

The lastIndexOf() Java Vector class method is used to get the index of the last occurrence of the specified element in the vector. There are two different types of Java lastIndexOf() method which can be differentiated depending on its parameter. These are:

  1. Java Vector lastIndexOf(Object o) Method
  2. Java Vector lastIndexOf(Object o, int index) Method

lastIndexOf(Object o) Method:

It returns the index of the last occurrence of the specified element in this vector. If the element is not found, it returns -1.

lastIndexOf(Object o, int index> c) Method:

This method is used to get the index of the last occurrence of the specified element in this vector. It starts searching for an element in the backward direction from the specified index. If the element is not found, it returns -1.

Syntax

Following is the declaration of lastIndexOf() method:

Parameter

Parameter Description Required/Optional
index It is an index where to start searching for an element in the backward direction. Required
obj It is an element to search for. Required

Return

The lastIndexOf() method returns the index of the last occurrence of the specified element in this vector or returns -1 if the vector does not contain the element.

Exceptions

IndexOutOfBoundsException- This method has thrown an exception if the index of an array is out of range i.e. (index >= size()).

Compatibility Version

Java 1.2 and above

Example 1

Test it Now

Output:

Index of element is: 3

Example 2

Test it Now

Output:

Index of element is: -1
The element is not found.

Example 3

Test it Now

Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException: 6 >= 5
	at java.base/java.util.Vector.lastIndexOf(Vector.java:469)
	at myPackage.VectorLastIndexOfExample3.main(VectorLastIndexOfExample3.java:14)

Example 4

Test it Now

Output:

Index of element is found at: 1

Next TopicJava Vector





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA