13. int lastIndexOf(Object o)

13. int lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this list,
or -1 if this list does not contain the element.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.spy.arraylist.method;
 
import java.util.ArrayList;
 
public class LastIndex 
{
 
	public static void main(String[] args) 
	{
 
		  ArrayList<String> emp_list=new ArrayList<>();
 
			emp_list.add("Bhavesh Lakhani");
			emp_list.add("Abhilash Shah");
			emp_list.add("Nilay Shah");
			emp_list.add("Bhavesh Lakhani");
			emp_list.add("Vaibhav Prajapati");
 
	 	   System.out.println("Retrieving element........");	
 
		 for (String s : emp_list) 
		 {
 
			 System.out.println(s);
 
		 }	
 
		//getting the last index of Object or element 
		int index= emp_list.lastIndexOf("Bhavesh Lakhani");
 
		System.out.println("last index of Bhavesh Lakhani: "+index);
 
 
	}
}

Output:

1
2
3
4
5
6
7
Retrieving element........
Bhavesh Lakhani
Abhilash Shah
Nilay Shah
Bhavesh Lakhani
Vaibhav Prajapati
last index of Bhavesh Lakhani: 3

Leave a Reply

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