Friday, 30 May 2014

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