Java

How to Get an Index of an Array Element in Java

An array is a set of elements with the same data type and is treated as a fixed-size data structure. In Java, an array inherits the Object class and can be created dynamically. However, arrays are index-based, that’s why the first element of an array is stored at the zero indexes and the second element is at the first index, and so on.

This guide will illustrate the methods to get an index of an array element in Java.

How to Get an Index of an Array Element in Java?

To get an index of an array element in Java, you can use the below-mentioned methods.

  • Linear search
  • indexOf() method
  • binarySearch() method

Let’s understand the working of these methods one by one with examples!

Method 1: Get an Index of an Array Element Using Linear Search

Linear Search is simple to implement in cases when an array has fewer elements. This type of search can be performed on both single-dimensional and multi-dimensional arrays. When the index of a single array element is being fetched from an unordered list, linear search is proved more effective.

In this section, we will get an index of an array element by using a linear search with a “for” loop.

Syntax
The following syntax is used for the specified purpose:

for(int i = 0; i < array.length; i++)

Here, the loop starts from “0” and it will iterate until the length of the array.

Example
In this example, we will create an integer type array named “array” and initialize it with the following values:

int[] array = {1,5,8,14,20,23};

We want to get the index of the array element “5”, so, we will store it in an integer type variable “item”:

int item = 5;

As we know, the index of an array starts from 0, so we save “-1” in an integer type variable “index” that will increment in the loop by iterating elements in an array:

int index = -1;

Here, we will iterate the array until the length of the array by using “for” loop and check the elements of the array to match with the variable “item” and store the matched “index” when the element of the array matches with “item”:

for(int i = 0; i < array.length; i++) {
     if(array[i] == item) {
            index = i;
            break;
      }
}

Finally, print the value of the “index” variable, which stores the index of the matched item:

System.out.println("5 is located at "+ index +" index");

The output shows that the “5” element of an array is stored at the “1” index:

Let’s see the other methods for getting an index of an array element in Java.

Method 2: Get an Index of an Array Element Using indexOf() Method

The “indexOf()” method of the “Arrays” class belongs to the java.util package and is also utilized to determine the particular element’s index in an array. This method fetches the index of the first instance of the added element. It first converts an array into an Array List using the “asList()” method and then calls the “indexof()” method.

Syntax
The syntax of the “indexOf()” method is given as:

Arrays.asList(array).indexOf(value);

Here, the “aslist()” method is called using the class name “Arrays” by passing an “array” to it as an argument, which converts it to an Array List and then calls the “indexOf()” method by passing the value for which we want to get the index.

Example
Firstly, create an integer type array named “array”. Note that there is an “Integer” wrapper class array, not a primitive “int” array, because the “aslist()” method takes a wrapper class as an argument, and it returns the primitive int values:

Integer[] array = {1,5,8,14,20,23};

To get the index of the “14” element, we will use the “indexOf()” method of the Array class, convert it in an Array List and then store the matched index in the “getArrayIndex” variable:

int getArrayIndex = Arrays.asList(array).indexOf(14);

Finally, print the index of an element using the “System.out.println()” method:

System.out.println("14 is located at "+ getArrayIndex +" index");

The output shows that the element “14” is placed at the “3” index of our array:

We checked the method to find the index of the element of an unsorted array. What if you have a sorted array? To get the answer to the stated question, follow the below-given sections.

Method 3: Get an Index of an Array Element in Java Using binarySearch() Method

The “binarySearch()” method is also used to get an index of the element of an array, but it is used for only sorted arrays. It is a predefined static method in Java that belongs to the Array class. It takes two parameters: an array and a value by which the index will be determined.

Note: The “binarySearch()” method is only used for the sorted array.

Syntax
Follow the below-given syntax for getting an index of an element of an array using binarySearch() method:

Arrays.binarySearch(array, value);

The specified method is called with the Array class, and it accepts a value that will be searched in the given array.

Example
Here, we have an array named “array” sorted in ascending order:

int[] array = {1,5,8,14,20,23};

We will call the “binarySearch()” method by passing the array and the element “20” as an argument. The resultant index will be saved in the integer type variable “index”:

int index = Arrays.binarySearch(array, 20);

Lastly, we will print the index of the specified element on the console window:

System.out.println("20 is located at "+ index +" index");

The output shows that the element “20” is located at the “4th” index of an array:

We gathered all the essential instructions for getting an index of an array element in Java.

Conclusion

For getting an index of an array element, you can use three different methods: Linear search by using a loop, indexOf() method, and binarySearch() method. The common and useful method to get the index of an element is the indexOf() method. Whereas the binarySearch() method is used on sorted arrays, and linear search can be utilized with the unsorted array. In this guide, we have illustrated the methods for getting an index of an array element in Java with detailed examples.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.