Sunday, 11 May 2014

LinkedList : indexOf : get the index of Element

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

import java.util.*;
 
class LinkedListIndexOf{
 public static void main(String args[]){
  LinkedList<Integer> myList;
  myList = new LinkedList<> ();
  
  /* Add Elements to myList */
  myList.add(10);
  myList.add(20);
  myList.add(30);
  
  System.out.println("Elements in myList are");
  System.out.println(myList);
  
  System.out.print("Index of 30 is ");
  System.out.println(myList.indexOf(30));
 }
}

Output
Elements in myList are
[10, 20, 30]
Index of 30 is 2


Prevoius                                                 Next                                                 Home

No comments:

Post a Comment