indexOf() method helps return the index of the first occurrence of the specified element. In this post, we are going to discuss the indexOf() method of the ArrayList class in detail.
- Method declaration – public int indexOf(Object o)
- What does it do? It will return the index of the first occurrence of the specified element. So, if an element is present multiple times in an ArrayList, then indexOf() will return the index of the first occurrence of that element in the list. And the indexing is 0-based, which means if the element is found at the 3rd location, then the function will return 2 as an index.
- What does it return? It will return an integer representing the index of the first occurrence of the specified element, and if that element is not found, it will return -1.
Code example
public class Codekru {
public static void main(String[] args) throws Exception {
ArrayList<String> al = new ArrayList<String>();
al.add("Hi");
al.add("codekru");
al.add("Hi");
al.add("again");
// printing arraylist contents
System.out.println(al);
System.out.println(al.indexOf("bye")); // will return -1 as "bye" don't exist in the list
System.out.println(al.indexOf("codekru")); // will return 1 as "codekru" is present at 2nd position
System.out.println(al.indexOf("Hi")); // will return 0, the first occurrence of "Hi" in the list
}
}
Output –
[Hi, codekru, Hi, again]
-1
1
0
Internal implementation of the ArrayList indexOf() method
public int indexOf(Object o) {
return indexOfRange(o, 0, size);
}
int indexOfRange(Object o, int start, int end) {
Object[] es = elementData;
if (o == null) {
for (int i = start; i < end; i++) {
if (es[i] == null) {
return i;
}
}
} else {
for (int i = start; i < end; i++) {
if (o.equals(es[i])) {
return i;
}
}
}
return -1;
}
indexOf() method internally calls the indexOfRange() method, which runs a for loop over the underlying array to find the first occurrence of the specified element.
Time complexity of the ArrayList indexOf() method
- The best time complexity for the indexOf() method would be O(1) when the element is located at the 1st position itself.
- The worst time complexity of the indexOf() method would be O(n) when the specified element is not found in the list or found at the last position.
- So, the average complexity would be O(n).
The What If scenarios
Q – What if we pass a different type of element in the function’s argument?
Here, our list is made up of strings, and we will pass an integer into the indexOf() method argument.
public class Codekru {
public static void main(String[] args) throws Exception {
ArrayList<String> al = new ArrayList<String>();
al.add("Hi");
al.add("codekru");
// printing arraylist contents
System.out.println(al);
System.out.println(al.indexOf(1)); // passed an integer in the argument
}
}
Output –
[Hi, codekru]
-1
It returned -1, as shown in the above output.
Q – What if we pass a null object in the function’s arguments?
indexOf() is also capable of finding a null object in an ArrayList.
public class Codekru {
public static void main(String[] args) throws Exception {
ArrayList<String> al = new ArrayList<String>();
al.add("Hi");
al.add(null);
al.add("codekru");
// printing arraylist contents
System.out.println(al);
System.out.println(al.indexOf(null)); // passed null in the argument
}
}
Output –
[Hi, null, codekru]
1
Here, it returned 1, the index of the null object in the ArrayList.
What if we try to use the indexOf() method on a null ArrayList?
It will throw a NullPointerException as illustrated by the below program.
public class Codekru {
public static void main(String[] args) throws Exception {
ArrayList<String> al = new ArrayList<String>();
al = null;
System.out.println(al.indexOf(null));
}
}
Output –
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.ArrayList.indexOf(Object)" because "al" is null
If we want the last occurrence of the specified element instead of the first occurrence, then we can use the lastIndexOf() method of the ArrayList class.
Please visit this link to learn more about the ArrayList class of java and its other functions or methods.
We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at admin@codekru.com.