In Java, indexOf(o) searches an ArrayList from the start and returns the index of the first element equal to o. If no element matches, it returns -1.
Like contains, it uses equals() and checks elements one by one, so the time grows with the list size. Use lastIndexOf to find the last match instead.
An instance method of java.util.ArrayList (Java 1.2+): a resizable-array list with index access that allows null and duplicates and is not thread-safe.
Syntax
Pass the value to find; check the result for -1.
Java
List<String> list = new ArrayList<>(Arrays.asList("a", "b", "a"));
int i = list.indexOf("a"); // 0 (first match)
int j = list.indexOf("b"); // 1
int k = list.indexOf("z"); // -1 (not found)
Call
What it does
list.indexOf(o)
Index of the first match, or -1.
list.indexOf(o) >= 0
The same test as contains(o).
list.lastIndexOf(o)
Index of the last match, or -1.
list.subList(from, list.size()).indexOf(o)
Searches from from onward; add from to a non-negative result.
List has no indexOf(o, fromIndex) overload, unlike String.indexOf. A subList view gives you the same effect.
Library declarations
Java
// java.util.ArrayList<E>
public int indexOf(Object o)
// Declared in List<E>
Parameters
Parameter
Description
o (Object)
The value to find. May be null.
Return Value
Return Value
Description
int
The index of the first element equal to o, or -1 if there is none.
Look up two days by name and use a found index to read its neighbor.
Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ArrayListIndexOfDemo {
public static void main(String[] args) {
List<String> days = new ArrayList<>(Arrays.asList("Mon", "Tue", "Wed", "Thu", "Fri"));
System.out.println("Wed at " + days.indexOf("Wed"));
System.out.println("Sun at " + days.indexOf("Sun"));
int pos = days.indexOf("Thu");
if (pos != -1) {
System.out.println("day after Thu: " + days.get(pos + 1));
}
}
}
Output
Wed at 2
Sun at -1
day after Thu: Fri
How It Works
Count from zero. Mon is 0 and Tue is 1, so Wed is at index 2.
Missing value. Sun is not in the list, so indexOf returns -1 instead of throwing.
Check before use. The if statement makes sure Thu was found before its index is used.
Neighbor lookup. Thu is at 3, so get(pos + 1) reads index 4, Fri.
Example 2 — Only the First Match Counts
Find the first error, then search the rest of the list for the next one.
Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ArrayListIndexOfDuplicates {
public static void main(String[] args) {
List<String> log = new ArrayList<>(Arrays.asList("ok", "error", "ok", "error", "ok"));
int first = log.indexOf("error");
int second = log.subList(first + 1, log.size()).indexOf("error") + first + 1;
System.out.println("first error at " + first);
System.out.println("second error at " + second);
System.out.println("last error at " + log.lastIndexOf("error"));
}
}
Output
first error at 1
second error at 3
last error at 3
How It Works
First hit.indexOf("error") stops at index 1, the first match, and ignores the rest.
Search after it.subList(2, 5) is a view of the elements after the first error; indexOf finds "error" at index 1 of that view.
Convert back. Adding first + 1 (the view's starting index, 2) turns view index 1 into list index 3.
Cross-check.lastIndexOf searches from the end and also lands on index 3, since there are only two errors.
Example 3 — indexOf with Numbers and null
Search an Integer list, including for null, and see why a Long list needs 42L.
Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ArrayListIndexOfNumbers {
public static void main(String[] args) {
List<Integer> ids = new ArrayList<>(Arrays.asList(500, 42, null, 42));
List<Long> bigIds = new ArrayList<>(Arrays.asList(500L, 42L));
System.out.println("42 in ids: " + ids.indexOf(42));
System.out.println("null in ids: " + ids.indexOf(null));
System.out.println("42 in bigIds: " + bigIds.indexOf(42));
System.out.println("42L in bigIds: " + bigIds.indexOf(42L));
}
}
Output
42 in ids: 1
null in ids: 2
42 in bigIds: -1
42L in bigIds: 1
How It Works
Boxing to search.ids.indexOf(42) boxes 42 into an Integer and finds the first match at index 1.
null is a value too.ArrayList allows null elements, and indexOf(null) finds the one at index 2.
Type mismatch. In a List<Long>, the int literal 42 is boxed to an Integer, and Long.equals(Integer) is always false, so the result is -1.
Use the right literal.42L boxes to a Long and is found at index 1.
Frequently Asked Questions
-1. Always check for -1 before using the result as an index.
The first. Use lastIndexOf() for the last occurrence.
equals(). Two different objects that are equal by value are considered a match.
Not directly. Search a view instead: list.subList(from, list.size()).indexOf(o), then add from to a result that is not -1.
The int 42 is boxed to an Integer, which never equals a Long. Pass 42L instead.