Java Array Indexof

  1. Get Index of an Element in an Integer Type Array in Java
  2. Get Index of an Array Element Using Java 8 Stream API in Java
  3. Get Index of an Array Element Using ArrayUtils.indexOf() in Java

This article introduces how to get the index of an array in Java using different techniques.

Get Index of an Element in an Integer Type Array in Java

There is no indexOf() method for an array in Java, but an ArrayList comes with this method that returns the index of the specified element. To access the indexOf() function, we first create an array of Integer and then convert it to a list using Arrays.asList().

Notice that we use a wrapper class Integer instead of a primitive int because asList() only accepts wrapper classes, but they do return the result as a primitive data type. We can check the following example, where we specify the element i.e. 8 to the indexOf() method to get its index. The result we get from getIndex is of the int type.

import java.util.Arrays;

public class ArrayIndexOf {
    public static void main(String[] args) {
        Integer[] array1 = {2, 4, 6, 8, 10};

        int getIndex = Arrays.asList(array1).indexOf(8);

        System.out.println("8 is located at "+getIndex+" index");
    }
}

Output:

8 is located at 3 index

Get Index of an Array Element Using Java 8 Stream API in Java

We can use the Stream API to filter out the array items and get the position of the specified element. IntStream is an interface that allows a primitive int to use the Stream functions like filter and range.

range() is a method of IntStream that returns the elements from the starting position till the end of the array. Now we use filter() that takes a predicate as an argument. We use i -> elementToFind == array1[i] as the predicate where i is the value received from range() and elementToFind == array1[i] is the condition to check if the elementToFind matches with the current element of the array1.

findFirst() returns the first element and orElse() returns -1 if the condition fails.

import java.util.stream.IntStream;

public class ArrayIndexOf {
    public static void main(String[] args) {
        int[] array1 = {1, 3, 5, 7, 9};

        int elementToFind = 3;

        int indexOfElement = IntStream.range(0, array1.length).
                filter(i -> elementToFind == array1[i]).
                findFirst().orElse(-1);

        System.out.println("Index of " + elementToFind + " is " + indexOfElement);

    }
}

Output:

Index of 3 is 1

Get Index of an Array Element Using ArrayUtils.indexOf() in Java

This example uses the ArrayUtls class that is included in the Apache Commons Library. We use the below dependency to import the library functions to our project.

  <dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.11</version>
  </dependency>

We use the indexOf() function of the ArrayUtils class to find the index of the array. indexOf() accepts two arguments, the first argument is the array, and the second argument is the element of which we want to find the index.

import org.apache.commons.lang3.ArrayUtils;

public class ArrayIndexOf {
    public static void main(String[] args) {
        int[] array1 = {1, 3, 5, 7, 9};

        int elementToFind = 9;

        int indexOfElement = ArrayUtils.indexOf(array1, elementToFind);
        System.out.println("Index of " + elementToFind + " is " + indexOfElement);

    }
}

Output:

Index of 9 is 4
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Java Array

  • Convert Int Array to Arraylist in Java
  • Check if an Array Contains Int in Java