Skip to main content

15. int indexOf(Object o)

15. int indexOf(Object o)
Returns the index of the first 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
package com.spy.method.LinkedList;
 
import java.util.ArrayList;
import java.util.LinkedList;
 
public class indexofMethod 
{
	public static void main(String[] args) 
	{
 
		LinkedList<String> emp_list=new LinkedList<>();
 
		emp_list.add("Bhavesh Lakhani");
		emp_list.add("Abhilash Shah");
		emp_list.add("Nilay Shah");
		emp_list.add("Vaibhav Prajapati");
 
 
	   int index=emp_list.indexOf("Bhavesh Lakhani");
 
	   System.out.println("Index number of Bhavesh Lakhani: "+index);
 
 
 
	   int i=emp_list.indexOf("Vaibhav Prajapati");
 
	   System.out.println("Index number of Vaibhav Prajapati: "+i);
 
	}
}

output

1
2
Index number of Bhavesh Lakhani: 0
Index number of Vaibhav Prajapati: 3