Friday 30 May 2014

ArrayList : 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 ArrayListLastIndexOf{
 public static void main(String args[]){
  ArrayList<Integer> myList;
  myList = new ArrayList<> ();
  
  /* 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