Java array indexOf example shows how to find index of element in array. The example also shows how to find index of element in primitive as well as object arrays and custom Java array indexOf method implementation.
How to find the index of an element in an array?
Java arrays are objects, however, they do not provide an indexOf method. In order to search the element and get its index, we need to implement our own indexOf method. Here is the sample code for that.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
package com.javacodeexamples.basic.arrayexamples; import java.util.Arrays; public class ArrayIndexOfExample { public static void main(String[] args) { //String array containing months String[] strMonths = {"January", "March", "August"}; System.out.println("Index of March is: " + indexOf(strMonths, "March")); System.out.println("Index of October is: " + indexOf(strMonths, "October")); } private static int indexOf(Object[] strArray, Object element){ /* * Convert array to List and then * use indexOf method of List class. */ int index = Arrays.asList(strArray).indexOf(element); return index; } } |
Output
1 2 |
Index of March is: 1 Index of October is: -1 |
We first converted the array to List using the asList
method of the Arrays class. This method returns the List wrapper of the existing array. Since the List class has an indexOf method, we can use it to find an element index.
Please note that this code does not work for arrays of primitive types like int, float, char, double, etc. For primitive types like these, we need to write our own indexOf method per type. The below-given code example shows the custom indexOf method for the int type.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
public class ArrayIndexOfExample { public static void main(String[] args) { int[] iArray = new int[]{1, 2, 3, 4, 5}; System.out.println("int array indexof 4: " + indexOf(iArray, 4)); System.out.println("int array indexof 11: " + indexOf(iArray, 11)); } private static int indexOf(int[] iArray, int element){ int index = -1; if(iArray == null) return index; for(int i=0; i < iArray.length; i++){ if(iArray[i] == element){ index = i; break; } } return index; } } |
Output
1 2 |
int array indexof 4: 3 int array indexof 11: -1 |
Additionally, If the array is sorted, you can also use the binarySearch
method of Arrays class as given below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.Arrays; public class ArrayIndexOfExample { public static void main(String[] args) { int[] iArray = new int[]{1, 2, 3, 4, 5}; System.out.println("int array indexof 2: " + indexOf(iArray, 4)); System.out.println("int array indexof 11: " + indexOf(iArray, 11)); } private static int indexOf(int[] iArray, int element){ Arrays.sort(iArray); return Arrays.binarySearch(iArray, element); } } |
Output
1 2 |
int array indexof 2: 3 int array indexof 11: -11 |
It is important to note that the above method,
1) Changes the original array by sorting it
2) It does not always return -1 if the element is not found, but a negative value according to the insertion point as you can see from the output.
3) It works for both primitives as well as Objects. If the array is of a custom type, the custom type must implement the Comparable or you can use an overloaded binarySearch
method which accepts Comparator argument.
And finally, if you are using the Apache Commons, you can use the indexOf
method of ArrayUtils class as given below.
1 2 3 4 5 6 7 8 9 10 11 |
import org.apache.commons.lang3.ArrayUtils; public class ArrayIndexOfExample { public static void main(String[] args) { int[] iArray = new int[]{1, 2, 3, 4, 5}; System.out.println("int array indexof 2: " + ArrayUtils.indexOf(iArray, 4)); System.out.println("int array indexof 11: " + ArrayUtils.indexOf(iArray, 11)); } } |
Output
1 2 |
int array indexof 2: 3 int array indexof 11: -1 |
This example is a part of the Java Array tutorial.
Please let me know your views in the comments section below.