Java.util.ArrayList
class method indexOf(Object o)
is used to find out the index of a particular element in a list.
Method indexOf() Signature
public int indexOf(Object o)
This method returns -1 if the specified element is not present in the list.
ArrayList indexOf() Method example
In the following example we have an arraylist of strings and we have added few elements to the arraylist. Here we are using the indexOf() method to find the index of few specified elements in the ArrayList.
import java.util.ArrayList; public class IndexOfExample { public static void main(String[] args) { ArrayList<String> al = new ArrayList<String>(); al.add("AB"); al.add("CD"); al.add("EF"); al.add("GH"); al.add("IJ"); al.add("KL"); al.add("MN"); System.out.println("Index of 'AB': "+al.indexOf("AB")); System.out.println("Index of 'KL': "+al.indexOf("KL")); System.out.println("Index of 'AA': "+al.indexOf("AA")); System.out.println("Index of 'EF': "+al.indexOf("EF")); } }
Output:
Index of 'AB': 0 Index of 'KL': 5 Index of 'AA': -1 Index of 'EF': 2
As you can see in the output that the index of element ‘AA’ is returned as -1 because this element does not exist in the ArrayList.
ArrayList indexOf() Method example if duplicate elements are present
In the following example, the element 90 is present twice in ArrayList, at the index 0 and index 3. When we used the indexOf() method to find the index of 90, we got the index of first occurrence of 90, this is because this method returns the index of first occurrence of the specified element. If you want to find out the last occurrence of the specified element use lastIndexOf() method instead.
import java.util.ArrayList; public class JavaExample { public static void main(String[] args) { ArrayList<Integer> al = new ArrayList<Integer>(); al.add(90); al.add(15); al.add(20); al.add(90); System.out.println("Index of 90: "+al.indexOf(90)); System.out.println("Index of 20: "+al.indexOf(20)); System.out.println("Index of 15: "+al.indexOf(15)); } }
Output:
Reference:
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#indexOf(java.lang.Object)
Divya says
If I want to take the ekement from user and for that element I want to find the index??
Its something like this
String s=s.next();
I want to find the index for s
Chaitanya Singh says
Yeah you can do that. Just pass the s in the method like this: int pos = al.indexOf(s);
rohith says
can we print the indices of all the elements present in array list?
Chaitanya Singh says
Yeah we can but why would you want to do that? The purpose of indexOf() method is to find an element in a List, what is the point to find the position of all the elements. You can print an entire ArrayList if you want.
shruti says
Get the each index value of 10 present in below list
Input: List = 10,20,30,90,10,10,40,50,10
Chaitanya Singh says
It will be 0 because the indexOf() method returns the index of first occurrence of the element.
zubair ahmed says
well how come index of aa is -1…….
you can check out my code..
ArrayList list1 = new ArrayList(3);
String x= list1.get(0);
int i= list1.indexOf(x);
System.out.println(i)
which will give you 0…
Chaitanya Singh says
Your code gave you 0 because you are finding the index of the first element. The index of first element is 0 in ArrayList. The example which I have shared above returns -1 for element ‘AA’ because the element doesn’t exist in the ArrayList. The indexOf() method returns -1 if the element is not present in the List.