Core Java Tutorial

Core Java Tutorial

LinkedList Example Program for indexOf(Object o)

[java]
/**
Author :candidjava.com
Description: Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
*/
import java.io.IOException;
import java.util.*;
public class LinkedListIndexOf
{
public static void main(String args[])
{
boolean a;
int b=60,c=20,d=30,e,f=60;
LinkedList l=new LinkedList();
a=l.add(b);
a=l.add(c);
a=l.add(d);
e=l.indexOf(f);//returns the position of the element in the list
System.out.println("position of the given element in the list is " +e);
}
}
[/java]

Output:

position of the given element in the list is
0