Java find index of element in array – Java Program to Find the Index of an Array Element

Java find index of element in array: In the previous article, we have seen Java Program to Check if an Array Contains a Specific Value

In this article we are going to find index of an array element using Java programming language.

Java Program to Find the Index of an Array Element

Find index of element in array java: Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to find to find index of an array element.

Method-1: Java Program to Find the Index of an Array Element By Using Linear Approach

Approach: 

  • Create an array of elements.
  • Display the array.
  • Ask the user to enter a value, of which we will search and find the index.
  • Traverse through the array to find elements matching.
  • If the searched element is found in the array then print the index where it is present.
  • If the searched element is not found then print that Index not found for that element.

Program:

import java.util.*;
public class Main
{    
    public static void main(String args[])
    {
        // Crating an array
        int arr[] = { 9, 5, 7, 8, 6, 3};
        System.out.print("Array: ");
        printArray(arr);
        // Taking item input
        System.out.print("Enter searched element: ");
        Scanner scan = new Scanner(System.in);
        int item = scan.nextInt();
        // Traversinng the array looking for the element
        int index = -1;
        for(int i = 0; i<arr.length; i++)
        {
            if(arr[i]==item)
            {
                index = i;
                break;
            }

        }
        // Printing the final result
        if(index == -1)
            System.out.println("Index not found for the entered element "+item);
        else
            System.out.println("Entered element "+item+" is present at index "+index);
    }

    // Function to print the array
    static void printArray(int arr[])
    {
        for(int i = 0; i < arr.length ; i++)
            System.out.print(arr[i]+" ");
        System.out.println();
    }
}
Output:

Case-1

Array: 9 5 7 8 6 3 
Enter searched element: 7
Entered element 7 is present at index 2

Case-2

Array: 9 5 7 8 6 3 
Enter item to check if it is present or not: 45
Index not found for the entered element 45

Method-2: Java Program to Find the Index of an Array Element By Using Binary Approach

Approach: 

  • Create an array.
  • Sort the array.
  • Implement Binary Search algorithm to check if the searched element is present or not.
  • If the searched element is present then print the index of element where it is present.
  • If the searched element is not found then print that Index not found for that element.

Program:

import java.util.*;
public class Main
{    
    public static void main(String args[])
    {
        // Crating an array
        int arr[] = { 9, 5, 7, 8, 6, 3};
        // Sorting the array
        Arrays.sort(arr);
        System.out.print("Array: ");
        printArray(arr);
        // Taking item input
        System.out.println("Enter item to search: ");
        Scanner scan = new Scanner(System.in);
        int item = scan.nextInt();
        
        // Calling Binarysearch function
        int index = binSearch(arr,0,arr.length,item);
        
        // Printing the final result
        if(index == -1)
            System.out.println("Index not found for the entered element "+item);
        else
            System.out.println("Entered element "+item+" is present at index "+index);
    }

    // Function to print the array
    static void printArray(int arr[])
    {
        for(int i = 0; i < arr.length ; i++)
            System.out.print(arr[i]+" ");
        System.out.println();
    }
    // Binary Search Algorithm
    // Checks whether the element is present inside the sorted array
    static int binSearch(int arr[], int l, int r, int item)
    {
        if (r >= l) {
            int mid = l + (r - l) / 2;
            if (arr[mid] == item)
                return mid;
            if (arr[mid] > item)
                return binSearch(arr, l, mid - 1, item);

            return binSearch(arr, mid + 1, r, item);
        }
        return -1;
    }
}
Output:

Array: 3 5 6 7 8 9 
Enter item to search: 
9
Entered element 9 is present at index 5

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Related Java Programs: