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
🌐
W3Schools
w3schools.com › java › ref_arraylist_indexof.asp
Java ArrayList indexOf() Method
public int indexOf(Object item) Java Arrays Tutorial · Java ArrayList Tutorial · ❮ ArrayList Methods · ★ +1 · Sign in to track progress · REMOVE ADS · PLUS · SPACES · GET CERTIFIED · FOR TEACHERS · BOOTCAMPS · CONTACT US · × · If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com ·
🌐
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 java.util.ArrayList; public class Geeks { public static int findIndex(int arr[], int t){ ArrayList<Integer> clist = new ArrayList<>(); // adding elements of array // to ArrayList for (int i : arr) clist.add(i); return clist.indexOf(t); } public static void main(String[] args){ int[] a = { 5, 4, 6, 1, 3, 2, 7, 8, 9 }; int t = 7; System.out.println(findIndex(a, t)); } }
🌐
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.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › util › Arrays.html
Arrays (Java Platform SE 8 )
April 21, 2026 - Java™ Platform Standard Ed. 8 ... This class contains various methods for manipulating arrays (such as sorting and searching).
Find elsewhere
🌐
FavTutor
favtutor.com › blogs › java-indexof
indexOf Method in Java (with Examples)
November 18, 2023 - The indexOf method in Java is a vital utility used to locate the index position of a particular element or substring within various data structures such as strings and arrays. It is part of the String class and List interface, offering a convenient ...
🌐
Codecademy
codecademy.com › docs › java › arraylist › .indexof()
Java | ArrayList | .indexOf() | Codecademy
February 12, 2023 - The .indexOf() method returns the index of the first occurrence of the specified element in an ArrayList. If the element is not found, -1 is returned. ... Looking for an introduction to the theory behind programming?
🌐
Medium
medium.com › @AlexanderObregon › javas-arraylist-indexof-method-explained-6bb47ce4c894
Java’s ArrayList.indexOf() Method Explained | Medium
August 19, 2024 - This method accepts a single argument, Object o, which represents the element you wish to find within the ArrayList. If the specified element is present in the list, indexOf() returns the zero-based index of its first occurrence.
🌐
Coderanch
coderanch.com › t › 454731 › java › Printing-index-array
Printing the index of an array (Beginning Java forum at Coderanch)
July 19, 2009 - Mike Osterhout wrote:the indexes of the array. Arrays are indexed via a number -- so an array of size 5 has the indexes 0, 1, 2, 3, and 4. Is that what you want? Henry · Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
🌐
TutorialsPoint
tutorialspoint.com › article › find-the-index-of-an-array-element-in-java
Find the Index of an Array Element in Java
July 31, 2023 - If the component isn't found, the indexOf strategy returns -1. import java.util.ArrayList; import java.util.List; public class ArrayIndexFinder { public static int findIndexApproach3(int[] array, int element) { List<Integer> list = new ArrayList<>(); for (int i : array) { list.add(i); } return list.indexOf(element); } public static void main(String[] args) { int[] array = {1, 2, 3, 4, 5}; int element = 4; int index = findIndexApproach3(array, element); System.out.println("Index of element " + element + ": " + index); } }
🌐
Codemia
codemia.io › knowledge-hub › path › where_is_javas_array_indexof
Where is Java's Array indexOf?
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
NIST
math.nist.gov › javanumerics › array › doc › array.Index.html
Class array.Index
java.lang.Object | +----array.Index · public final class Index · extends Object Index = represents an index set to arrays · Ranges provide the support to extract and operate on regular array sections. Indexes provide similar support for irregular sections.
🌐
Coderanch
coderanch.com › t › 622130 › java › ArrayList-Find-Indexes-Occurrence
ArrayList: Find all Indexes of an Occurrence [Solved] (Beginning Java forum at Coderanch)
October 19, 2013 - And, more importantly, what does it take as a parameter? What is your for loop doing? indexOf() is supposed take in the index of i in the array. My for loop takes me through the entire array. On a side note, this is for an assignment, and we're required to use .equals as part of this code.
🌐
Programiz
programiz.com › java-programming › library › arraylist › indexof
Java ArrayList indexOf()
returns the position of the specified element from the arraylist · 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 ...
🌐
How to do in Java
howtodoinjava.com › home › collections framework › java arraylist › java arraylist.indexof()
Java Arraylist.indexOf() - Get Element Index - HowToDoInJava
January 13, 2023 - ArrayList<String> list = new ArrayList<>(Arrays.asList("alex", "brian", "charles","alex",","alex","harry")); int index = list.indexOf("alex"); System.out.println(index);
🌐
OneCompiler
onecompiler.com › questions › 3xmsragec › -java-how-to-find-the-index-of-given-element-in-an-array
[Java] How to find the index of given element in an array? - Questions - OneCompiler
List has indexOf method, so we can convert Array to List and call indexOf method on it. Following code shows how to do that. import java.util.Arrays; public class JavaArrays { public static void main(String[] args) { String[] colors = { "red", "green", "blue", "orange", "maroon" }; System.out.println(Arrays.asList(colors).indexOf("blue")); } }
🌐
LeetCode
leetcode.com › problems › peak-index-in-a-mountain-array
Peak Index in a Mountain Array - LeetCode
Can you solve this real interview question? Peak Index in a Mountain Array - You are given an integer mountain array arr of length n where the values increase to a peak element and then decrease. Return the index of the peak element. Your task is to solve it in O(log(n)) time complexity.