Java ArrayList indexOf Example
ArrayList class indexOf method example. This example shows you how to use indexOf method.
ArrayList class indexOf method example.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.
Here is the code
/**
* @ # indexOf.java
* A class repersenting use to indexOf method of ArrayList class in java.util package
* version 14 May 2008
* author Rose India
*/
import java.util.*;
public class IndexOf {
public static void main(String args[]) {
List arrlist = new ArrayList();
int i = 0;
String str = "Rose";
//Add the specified element to the end of this list.
arrlist.add(i);
arrlist.add(str);
arrlist.add("India");
System.out.println(arrlist);
System.out.println("Index of the specified element in the ArrayList = " + arrlist.indexOf("India"));
}
} |
Output
[0, Rose, India]
Index of the specified element in the ArrayList = 2 |
|