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 ofequals()
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
Parameter | Description |
---|---|
o | The 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 firstnull
element, if present.
How Does indexOf() Work Internally?
- The method loops through the list from index 0 to
size() - 1
and checks each element withequals()
. - 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
Aspect | Details |
---|---|
Method Purpose | Find the index of the first occurrence of an element |
Syntax | list.indexOf(Object o) |
Return Value | Index if found, -1 if not found |
Parameters | The element to search |
Throws Exception | None |
Case Sensitivity | Yes (for String and similar types) |
Time Complexity | O(n) (linear search) |
Typical Use Cases | Checking 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.