Skip to content

WebDevHubs

  • Home
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • Java
  • Selenium
  • PHP
  • Python
  • Programs
  • Toggle search form

Java ArrayList indexOf() Method

Posted on July 23, 2025July 23, 2025 By Admin No Comments on Java ArrayList indexOf() Method

The indexOf() method is a fundamental operation of the Java ArrayList class. It allows you to find the index of the first occurrence of a specified element in the list. This capability is essential whenever you need to locate an item’s position, check for its existence, or work with elements dynamically in your program.

What Is the indexOf() Method in ArrayList?

  • The indexOf() method returns the index of the first occurrence of a specified element in the list.
  • If the element is not found, it returns -1.
  • The method uses the equals() method to compare list elements, so proper implementation of equals() is important for custom objects.
  • The search starts from the beginning of the list (index 0).

Syntax

public int indexOf(Object o)

Where:

  • o is the element you want to search for in the list.

Parameters

ParameterDescription
oThe element for which to search in the list.

Return Value

  • Returns the index of the first occurrence of the specified element.
  • Returns -1 if the element is not present in the list.

Exceptions

  • No specific exceptions are thrown for invalid input or if the element is not found.
  • The method accepts null as an argument and will return the index of the first null element, if present.

How Does indexOf() Work Internally?

  • The method loops through the list from index 0 to size() - 1 and checks each element with equals().
  • When a match is found, the current index is returned.
  • If no match exists, -1 is returned.
  • This is a linear search with time complexity O(n), where n is the size of the list.

Examples of the indexOf() Method

1. Find the Index of a String in ArrayList

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<String> colors = new ArrayList<>();
        colors.add("Red");
        colors.add("Green");
        colors.add("Blue");
        colors.add("Green");

        int pos = colors.indexOf("Green");
        System.out.println("First Green at index: " + pos); // Output: 1
    }
}

Output:

First Green at index: 1

2. Element Not Present in the List

import java.util.ArrayList;

public class Demo {
    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(10);
        numbers.add(20);
        numbers.add(30);

        int pos = numbers.indexOf(40); // Not in list
        System.out.println("Index of 40: " + pos); // Output: -1
    }
}

3. Using indexOf() with Custom Objects

import java.util.ArrayList;

class Book {
    String title;
    Book(String title) {
        this.title = title;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) return true;
        if (!(obj instanceof Book)) return false;
        Book other = (Book) obj;
        return this.title.equals(other.title);
    }
}

public class Example {
    public static void main(String[] args) {
        ArrayList<Book> books = new ArrayList<>();
        books.add(new Book("Java"));
        books.add(new Book("Python"));

        Book search = new Book("Java");
        int index = books.indexOf(search);
        System.out.println("Java book at index: " + index); // Output: 0
    }
}

4. Find Index of null Element

import java.util.ArrayList;

public class TestNull {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        list.add(null);
        list.add("Hello");
        System.out.println(list.indexOf(null)); // Output: 0
    }
}

Important Notes

  • First Occurrence Only: If the element appears multiple times, indexOf() returns only the first occurrence.
  • Not Found: Returns -1 if the element does not exist in the list.
  • Custom Objects: Make sure to override equals() for correct matching if searching with custom types.
  • Null Elements: Works with null as a valid argument and returns its first index, if present.
  • Case Sensitivity: For objects like String, indexOf() is case-sensitive ("green" and "Green" are different).

Summary

AspectDetails
Method PurposeFind the index of the first occurrence of an element
Syntaxlist.indexOf(Object o)
Return ValueIndex if found, -1 if not found
ParametersThe element to search
Throws ExceptionNone
Case SensitivityYes (for String and similar types)
Time ComplexityO(n) (linear search)
Typical Use CasesChecking position, existence tests, removal by index

Typical Use Cases

  • Checking if an element exists before removing or updating it by index.
  • Getting the position of user input or searched data in the list.
  • Finding the location of duplicate or specific items.
Java Tags:Java-ArrayList, Java-ArrayList-Method, Java-Collections

Post navigation

Previous Post: Java ArrayList get() Method
Next Post: Java ArrayList isEmpty() Method

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Java ArrayList
  • Java ArrayList Methods
  • Java ArrayList add() Method
  • Java ArrayList addAll() Method
  • Java ArrayList clear() Method
  • Java ArrayList clone() Method
  • Java ArrayList contains() Method
  • Java ArrayList ensureCapacity() Method
  • Java ArrayList forEach() Method
  • Java ArrayList get() Method
  • Java ArrayList indexOf() Method
  • Java ArrayList isEmpty() Method
  • Java ArrayList iterator() Method
  • Java ArrayList lastIndexOf() Method
  • Java ArrayList listIterator() Method
  • Java ArrayList remove() Method
  • Java ArrayList removeAll() Method
  • Java ArrayList removeIf() Method
  • Java ArrayList replaceAll() Method
  • Java ArrayList retainAll() Method
  • Java ArrayList set() Method
  • Java ArrayList size() Method
  • Java ArrayList sort() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList subList() Method
  • Java ArrayList toArray() Method
  • Java ArrayList trimToSize() Method

Recent Posts

  • Java ArrayList trimToSize() Method
  • Java ArrayList toArray() Method
  • Java ArrayList subList() Method
  • Java ArrayList spliterator() Method
  • Java ArrayList sort() Method

Recent Comments

No comments to show.

Important Pages

  • About Us
  • Contact Us
  • Terms of Use
  • Privacy Policy

Web Development

  • HTML
  • CSS
  • JavaScript
  • PHP

Programming Languages

  • Java
  • Python
  • PHP
  • Programs

Others

  • Selenium
  • Lodash
  • Java ArrayList
  • JavaScript Array Methods

Copyright © 2025 WebDevHubs.

Powered by PressBook Green WordPress theme