Sunday, 11 May 2014

LinkedList : lastIndexOf : Get the last occurrence index of Element

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

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

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



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment