Find the Index of an Array Element in Java


When working with arrays in Java, it is frequently essential to discover the record of a particular component inside the array. This record can be utilized to get to or control the component as required. In this article, we'll investigate distinctive approaches to discover the file of an array component in Java, alongside their particular algorithms and case code.

Syntax

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
}

Explanation of Syntax

  • int[] array − The array in which we want to find the index of the element.

  • int element − The element whose index we want to find.

  • for loop − Iterates over each element of the array.

  • if statement − Compares each element with the desired element.

  • return i − Returns the index of the element if a match is found.

  • return -1 − Returns -1 if the element is not found in the array.

Approach 1

Algorithm

  • Start iterating over the array from the first element.

  • Compare each element with the desired element.

  • If a coordinate is found, return the file of the element.

  • If the conclusion of the array comes without finding a match, return -1.

Example

public class ArrayIndexFinder {

   public static int findIndexApproach1(int[] array, int element) {
      for (int i = 0; i < array.length; i++) {
         if (array[i] == element) {
            return i;
         }
      }
      return -1; // Element not found
   }

   public static void main(String[] args) {
      int[] array = {1, 2, 3, 4, 5};
      int element = 3;
      int index = findIndexApproach1(array, element);
      System.out.println("Index of element " + element + ": " + index);
   }
}

Output

Index of element 3: 2

Explanation

In this approach, we utilize a straightforward for loop to emphasize over the array. Interior the loop, we compare each component with the required component utilizing the in case explanation. On the off chance that a coordinate is found, we return the list of that component. In case the conclusion of the array is come to without finding a coordinate, we return -1 to demonstrate that the component was not found.

Approach 2

Algorithm

  • Use the Arrays lesson from the Java standard library to look for the component within the array.

  • If a match is found, return the list of the component.

  • If the component isn't found, the binarySearch strategy returns a negative value. Change over this value to the inclusion point by utilizing the bitwise complement (~) administrator and subtracting 1 to induce the list where the component would be embedded.

  • If the component is not found then it will return -1.

Example

import java.util.Arrays;

public class ArrayIndexFinder {

   public static int findIndexApproach2(int[] array, int element) {
      int index = Arrays.binarySearch(array, element);
      if (index >= 0) {
         return index;
      } else {
         return -1; // Element not found
      }
   }

   public static void main(String[] args) {
      int[] array = {1, 2, 3, 4, 5};
      int element = 3;
      int index = findIndexApproach2(array, element);
      System.out.println("Index of element " + element + ": " + index);
   }
}

Output

Index of element 3: 2

Explanation

This approach utilizes the binarySearch method from the Arrays class in the Java standard library. On the off chance that the component is found, the strategy returns its file specifically. On the off chance that the component isn't found, the strategy returns a negative esteem. We change over this negative esteem to the addition point by utilizing the bitwise complement administrator (~) and subtracting 1. At long last, we return -1 if the component isn't found.

Approach 3

Algorithm

  • Use the ArrayList class from the Java standard library to change over the array into an ArrayList.

  • Use the indexOf strategy of the ArrayList course to discover the list of the components.

  • If the component is found, return its list.

  • If the component isn't found, the indexOf strategy returns -1.

Example

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);
   }
}

Output

Index of element 4: 3

Explanation

In this procedure, we begin with turning the array into an ArrayList by repeating through the array and including each section to the list. The list of the component is at that point decided utilizing the indexOf work of the ArrayList lesson. In case the component is found, the record is returned. The strategy returns -1 on the off chance that the component cannot be found.

Approach 4

Algorithm

  • Start iterating over the array from the first element.

  • Compare each element to the required element using a while loop.

  • In the event that a coordinate is found, return the element's list.

  • In the event that the array's conclusion is come to without a coordinate, return -1.

Example

public class ArrayIndexFinder {

   public static int findIndexApproach4(int[] array, int element) {
      int i = 0;
      while (i < array.length) {
         if (array[i] == element) {
            return i;
         }
         i++;
      }
      return -1; // Element not found
   }

   public static void main(String[] args) {
      int[] array = {1, 2, 3, 4, 5};
      int element = 5;
      int index = findIndexApproach4(array, element);
      System.out.println("Index of element " + element + ": " + index);
   }
}

Output

Index of element 5: 4

Explanation

To repeat over the array in this strategy, we utilize a whereas loop instead of a for loop. Utilizing the on the off chance that articulation, we compare each component within the loop to the specified component. In case a coordinate is recognized, the list of that component is returned. In case we reach the conclusion of the array without finding a coordinate, we return -1.

Conclusion

In this article, we explored different ways to discover the record of an array component in Java. We discussed four approaches along with their algorithms and provided ready-to-execute code examples. Whether you prefer a simple for loop, utilizing the Arrays class, using an ArrayList, or a while loop, you now have multiple options to find the index of an array element based on your specific requirements.

Updated on: 31-Jul-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements