Find index of element in array Java
In this post, you will learn how to find the index of an element in an array using the Java programming language.
An array is a data structure representing an ordered collection of indexed items. In the development process, we may come to the situation where we need to find the index of an element in an array. Java provides various approaches to achieve this task. Here, we have mentioned most of them.
Java find the index of element in an array using while loop
In the given Java program, we traverse the array elements using the while loop, and when we find a match, we get the required index.
public class FindArrayIndex {
public static void main(String[] args) {
int[] arr = {33, 54, 22, 21, 89};
int element = 21;
int index = -1;
int i = 0;
while(i < arr.length) {
if(arr[i] == element) {
index = i;
break;
}
i++;
}
System.out.println("Index of "+element+" : "+index);
}
}
Output of the above code:
Index of 21 : 3
Java find the index of element in an array using for loop
In the given Java program, we traverse the array elements using the for loop, and when we find a match, we get the required index.
public class FindArrayIndex {
public static void main(String[] args) {
int[] arr = {13, 74, 63, 25, 93};
int element = 25;
int index = -1;
for(int i = 0; i < arr.length; i++) {
if(arr[i] == element) {
index = i;
break;
}
}
System.out.println("Index of "+element+" : "+index);
}
}
Output of the above code:
Index of 25 : 3
Java find the index of element in an array using recursion
In the given Java program, we create a recursion function getIndex() to find the first index of the given element.
public class FindArrayIndex {
public static int getIndex(int arr[], int ele, int start)
{
// start equals the length of the
// array, returns -1
if(start==arr.length)
return -1;
// if element at index start equals ele,
// returns start
if(arr[start]==ele)
return start;
// search for the rest
// position in the array
return getIndex(arr,ele,start+1);
}
public static int findIndex(int arr[], int ele)
{
return getIndex(arr,ele,0);
}
public static void main(String[] args) {
int[] x_arr = { 78, 23, 54, 11, 89, 50 };
System.out.println("Index of 11 : "
+ findIndex(x_arr, 11));
System.out.println("Index of 50 : "
+ findIndex(x_arr, 50));
}
}
Output of the above code:
Index of 11 : 3
Index of 50 : 5
Java find the index of element in an array using ArrayList
In the given Java program, we first convert the array into ArrayList, and then we use the indexOf() method of the ArrayList to get the index of the element.
import java.util.ArrayList;
public class FindArrayIndex {
public static int getIndex(int arr[], int ele)
{
// Creating ArrayList
ArrayList arrList = new ArrayList<>();
// adding elements of array
// to ArrayList
for (int i : arr)
arrList.add(i);
// returning index of the element
return arrList.indexOf(ele);
}
public static void main(String[] args) {
int[] x_arr = { 78, 23, 50, 22, 89, 50 };
System.out.println("Index of 22 : "
+ getIndex(x_arr, 22));
System.out.println("Index of 78 : "
+ getIndex(x_arr, 78));
}
}
Output of the above code:
Index of 22 : 3
Index of 78 : 0
Java find the index of element in an array using Steam API
Java 8 introduces the Stream API. The Stream API provides many methods that can be pipelined to produce the desired result. IntStream is part of the java.util.stream package. In the given Java program, we use the IntStream to find the index of an element.
import java.util.stream.IntStream;
public class FindArrayIndex {
// Function to find the index of an element
public static int getIndex(int arr[], int ele)
{
int len = arr.length;
return IntStream.range(0, len)
.filter(i -> ele == arr[i])
.findFirst()
.orElse(-1); // If no element found
}
public static void main(String[] args) {
int[] x_arr = { 12, 52, 50, 31, 89, 50 };
System.out.println("Index of 52 : "
+ getIndex(x_arr, 52));
System.out.println("Index of 89 : "
+ getIndex(x_arr, 89));
}
}
Output of the above code:
Index of 52 : 1
Index of 89 : 4
Java find the index of element in an array using Linear search
Here, we use the linear search to find the index of an element in an array.
import java.util.*;
public class FindArrayIndex {
// Function to find the index of an element
public static int getIndex(int arr[], int ele)
{
if (arr == null) {
return -1;
}
// find length of array
int len = arr.length;
int i = 0;
// traverse in the array
while (i < len) {
if (arr[i] == ele) {
return i;
}
else {
i = i + 1;
}
}
return -1;
}
public static void main(String[] args) {
int[] x_arr = { 12, 52, 50, 31, 89, 55 };
System.out.println("Index of 31 : "
+ getIndex(x_arr, 31));
System.out.println("Index of 55 : "
+ getIndex(x_arr, 55));
}
}
Output of the above code:
Index of 31 : 3
Index of 55 : 5
Related Articles
Number pattern programs in JavaJava program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number