Java ArrayList indexOf() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
ArrayList

Definition

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)
CallWhat it does
list.indexOf(o)Index of the first match, or -1.
list.indexOf(o) >= 0The 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

ParameterDescription
o (Object)The value to find. May be null.

Return Value

Return ValueDescription
intThe index of the first element equal to o, or -1 if there is none.

Compare with Similar Methods

Finding values and positions:

MethodHow it differs
lastIndexOf(o)Searches from the end and returns the last match.
contains(o)Only answers yes or no.
get(i)The opposite direction: from an index to a value.
remove(o)Finds the first match and removes it.

Java Examples

Example 1 — Find Where a Value Is

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));
        }
    }
}

How It Works

  1. Count from zero. Mon is 0 and Tue is 1, so Wed is at index 2.
  2. Missing value. Sun is not in the list, so indexOf returns -1 instead of throwing.
  3. Check before use. The if statement makes sure Thu was found before its index is used.
  4. 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"));
    }
}

How It Works

  1. First hit. indexOf("error") stops at index 1, the first match, and ignores the rest.
  2. 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.
  3. Convert back. Adding first + 1 (the view's starting index, 2) turns view index 1 into list index 3.
  4. 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));
    }
}

How It Works

  1. Boxing to search. ids.indexOf(42) boxes 42 into an Integer and finds the first match at index 1.
  2. null is a value too. ArrayList allows null elements, and indexOf(null) finds the one at index 2.
  3. 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.
  4. 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.

Next: isEmpty()

Check whether the list has no elements.

isEmpty() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

10 people found this page helpful