There are a couple of ways to accomplish this using the Arrays utility class.

If the array is not sorted and is not an array of primitives:

java.util.Arrays.asList(theArray).indexOf(o)

If the array is primitives and not sorted, one should use a solution offered by one of the other answers such as Kerem Baydoğan's, Andrew McKinlay's or Mishax's. The above code will compile even if theArray is primitive (possibly emitting a warning) but you'll get totally incorrect results nonetheless.

If the array is sorted, you can make use of a binary search for performance:

java.util.Arrays.binarySearch(theArray, o)
Answer from Jeffrey Hantin on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › find-the-index-of-an-array-element-in-java
Find the Index of an Array Element in Java - GeeksforGeeks
July 11, 2025 - In this approach, we will convert the array into ArrayList, and then we will use the indexOf method of ArrayList to get the index of the element. ... // Java program to find index of an array // element using ArrayList indexOf() Method import ...
🌐
W3Schools
w3schools.com › java › ref_arraylist_indexof.asp
Java ArrayList indexOf() Method
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<String> cars = new ArrayList<String>(); cars.add("Volvo"); cars.add("BMW"); cars.add("Ford"); cars.add("Mazda"); System.out.println(cars.indexOf("Ford")); } }
🌐
GeeksforGeeks
geeksforgeeks.org › java › ints-indexof-function-guava-java
Ints indexOf() function | Guava | Java - GeeksforGeeks
July 11, 2025 - Example 1: Java · // Java code ... element in array and return -1 // if element is not found in the array int index = Ints.indexOf(arr, target); if (index != -1) { System.out.println("Target is present at index " + index); ...
🌐
W3Docs
w3docs.com › java
How to find the index of an element in an int array?
int[] array = {1, 2, 3, 4, 5}; int index = Arrays.indexOf(array, 3); // index is 2
🌐
Baeldung
baeldung.com › home › java › java array › find the index of an element in a java array
Find the Index of an Element in a Java Array | Baeldung
December 7, 2023 - After the input array is converted into a list, we use the indexOf() method to find the index of our target element.
Find elsewhere
🌐
Techie Delight
techiedelight.com › home › java › find index of an element in given array in java
Find index of an element in given array in Java | Techie Delight
July 7, 2026 - The idea is to convert the given array to a list and use the List.indexOf() method that returns the index of the first occurrence of the specified element in this list. ... For sorted arrays, we can use Binary Search Algorithm to search the ...
🌐
Delft Stack
delftstack.com › home › howto › java › java array indexof
How to Java Array Indexof | Delft Stack
February 2, 2024 - Additionally, if the element is not found in the list, the indexOf() method will return -1, indicating that the element is not present in the list. In Java, you can use the Stream API to efficiently filter out elements from an array and determine ...
🌐
TutorialKart
tutorialkart.com › java › java-array › java-array-index-of-element
How to find index of Element in Java Array?
November 23, 2020 - public class ArrayExample { public ... the index will have a value of -1. ArrayUtils.indexOf(array, element) method finds the index of element in array and returns the index....
🌐
w3resource
w3resource.com › java-exercises › array › java-array-exercise-6.php
Java - Find the index of an array element
May 9, 2025 - ... import java.util.OptionalInt; // Define a class named Main. public class Main { // Define a method 'findIndex' that searches for an integer 't' in an integer array 'my_array'. public static OptionalInt findIndex(int[] my_array, int t) { // Check ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-get-the-index-of-a-specified-element-in-an-array-in-java
How to Get the Index of a Specified Element in an Array in Java? - GeeksforGeeks
July 23, 2025 - For arrays of non-primitive types (like String or Integer), Arrays.asList() can be used to convert the array into a list. The indexOf() method of the list is then used to find the element's index.
🌐
Medium
medium.com › @AlexanderObregon › javas-arraylist-indexof-method-explained-6bb47ce4c894
Java's ArrayList.indexOf() Method Explained
August 19, 2024 - If the specified element is present in the list, indexOf() returns the zero-based index of its first occurrence. If the element is not found, the method returns -1. ... import java.util.ArrayList; public class Example { public static void ...
🌐
TutorialsPoint
tutorialspoint.com › find-the-index-of-an-array-element-in-java
Find the Index of an Array Element in Java
July 31, 2023 - To discover the list of an array element in Java, the language structure by and large includes repeating over the array and comparing each component with the specified value. Once a coordinate is found, the record is returned. The essential sentence structure is as takes after ? public static int findIndex(int[] array, int element) { for (int i = 0; i < array.length; i++) { if (array[i] == element) { return i; } } return -1; // Element not found }
🌐
TutorialsPoint
tutorialspoint.com › java › util › arraylist_indexof.htm
Java ArrayList indexOf() Method
The Java ArrayList indexOf(Object) method returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. This method is used to search an element within arraylist and in case of a
🌐
Programiz
programiz.com › java-programming › library › arraylist › indexof
Java ArrayList indexOf()
Note: If the specified element doesn't exist in the list, the indexOf() method returns -1. import java.util.ArrayList; class Main { public static void main(String[] args) { // create an ArrayList ArrayList<Integer> numbers = new ArrayList<>(); // insert element to the arraylist numbers.add(22); numbers.add(13); numbers.add(35); System.out.println("Number ArrayList: " + numbers); // find the position of 13 int position1 = numbers.indexOf(13); System.out.println("Index of 13: " + position1); // find the position of 50 int position2 = numbers.indexOf(50); System.out.println("Index of 50: " + position2); } }
🌐
Codemia
codemia.io › home › knowledge hub › where is java's array indexof?
Where is Java's Array indexOf? | Codemia
July 30, 2025 - Java arrays do not have an indexOf method like the ArrayList does, largely because they are a low-level data structure that prioritizes efficiency. Adding such methods could introduce overhead that counters the simplicity and performance benefits of arrays.