Java Arraylist indexOf() Method Last Updated : 10 Dec, 2024 Comments Improve Suggest changes Like Article Like Report The indexOf() method in Java is used to find the index of the first occurrence of a specified element in an ArrayList.Example 1: Here, we will use the indexOf() method to find the index of a specific element in an ArrayList. Java // Java program to demonstrate the // use of indexOf() method import java.util.ArrayList; public class GFG { public static void main(String[] args) { // Creating an ArrayList of Integers ArrayList<Integer> arr = new ArrayList<Integer>(5); // Adding elements to the ArrayList arr.add(1); arr.add(2); arr.add(3); // Printing the initial values // in the ArrayList System.out.print(""); for (Integer v : arr) { System.out.print(v + " "); } // Using indexOf() to // find the index of 3 int p = arr.indexOf(3); // Prints the index of the element System.out.println("\nThe element 3 is at index: " + p); } } Output1 2 3 The element 3 is at index: 2 Syntax of ArrayList.indexOf() Methodpublic int indexOf(Object o)Parameter: The object o whose index is to find.Return Type: An integer that represents the index of the first occurrence of the specified element in the list, or -1 if the element is not found.Example 2: Here, we use the indexOf() method to find the first occurrence and lastIndexOf() to find the last occurrence of a specific element. Java // Java program to demonstrate the use of // indexOf() and lastIndexOf() methods import java.util.ArrayList; public class GFG { public static void main(String[] args) { // Creating an ArrayList of Integers ArrayList<Integer> arr = new ArrayList<Integer>(6); // Adding elements to the ArrayList arr.add(1); arr.add(2); arr.add(4); arr.add(6); arr.add(5); arr.add(2); // Using indexOf() to find the // first index of 4 int p1 = arr.indexOf(4); // Using lastIndexOf() to find the // last index of 4 int p2 = arr.lastIndexOf(4); // Balancing the 0-based indexing p1 = p1 + 1; p2 = p2 + 1; // Printing first and last index of 4 System.out.println("The first occurrence of 4 is: " + p1); System.out.println("The last occurrence of 4 is: " + p2); } } OutputThe first occurrence of 4 is: 3 The last occurrence of 4 is: 3 Example 3: If the specified element is not found in the ArrayList, the indexOf() method will return -1. Java // Java program to demonstrate the use of // indexOf() method with a non-existent element import java.util.ArrayList; public class GFG { public static void main(String[] args) { // Creating an ArrayList of Integers ArrayList<Integer> arr = new ArrayList<Integer>(5); // Adding elements to the ArrayList arr.add(10); arr.add(20); arr.add(30); // Trying to find the index of // a non-existent element int p = arr.indexOf(50); System.out.println("The index of 50 is: " + p); } } OutputThe index of 50 is: -1 Comment More info S Shambhavi Singh 1 Improve Article Tags : Java Technical Scripter Java-Collections Java - util package Java-Functions Java-ArrayList +2 More Explore Java BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOP & InterfacesClasses and Objects in Java10 min readAccess Modifiers in Java6 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking15+ min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like