Table of contents
1.
Introduction
2.
What is Java Array Indexof ?
3.
Application of Java Array indexof Method 
4.
How to Get an Index of an Array Element in Java?
4.1.
Linear search 
4.2.
indexOf() method 
4.3.
binarySearch() method 
5.
Code to Demonstrate Java Array Indexof Method
6.
Frequently Asked Questions
6.1.
What is the Java array Indexof method?
6.2.
What is the application of the Java array Indexof method?
6.3.
Can you use indexOf on an array in Java?
6.4.
How to display array index in Java?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Java Array Indexof

Author Juhi Sinha
1 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

A collection of the same type of elements is known as an array. Once an array is created, its size cannot be changed.

Java Array Indexof

Java allocates memory when an array is created based on the array's size and the types of elements it will contain. An element is a discrete item contained within an array that can only be accessed using the array index.

In this blog, we will briefly discuss the Java Array Indexof method. So without any further ado, let’s get started!

You can also read about the topic of Java Destructor and Fibonacci Series in Java.

Recommended Topic-  Iteration Statements in Java.

What is Java Array Indexof ?

 

What is Java Array Indexof

Java doesn't have an indexOf() method for Arrays, but it does have one for ArrayLists, which returns the index of the specified element. We first create an array of integers and then use Arrays.asList() to convert it into a list so that we can access the indexOf() function.

The indexOf() method of ArrayList returns -1 if the element is not present in this list or the index of the first instance of the element in the list.

Must Read Difference between ArrayList and LinkedList and Hashcode Method in Java.

Application of Java Array indexof Method 

  • The indexof functions are most helpful for finding the last or first occurrence of events.
  • For example, last time the number 1 appears in a die throw or the first time any letter appears in a name.

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

Linear search 

In Java, you can perform a linear search on an array to find the index of a particular element by iterating through the array and comparing each element to the target element. Given implementation will hep you understand the Linear search:

public class Main {
   static int [] arr;
   public static void main(String[] args) {
       arr = new int[]{3,2,4,5,6,7};
       // Function call and the return value stored in 'i'
       int i = find_index(arr, 6);
       if(i == -1)
       {
           System.out.print("Element not contained by the array");         
       }
       else
       System.out.print("Element found at : "+i);  
   }
   
   private static int find_index(int [] arr,int j)
   {
       for(int i=0;i<arr.length;i++)
       {
       // comparing element to the target element
           if(arr[i]==j)
           {
               return i;
               
           }           
       }
       return -1;
       
   }
}

Output:

Element found at : 4

Time complexity: O(n)

indexOf() method 

In Java, the indexOf method of the Arrays class to find the index of a particular element in an array. The indexOf method returns index of the element first occurring in the array, or -1 if the element is not found.
Implementation:

import java.util.Arrays;
public class IndexOf {
   public static void main(String[] args) {
       Integer[] array1 = {2, 4, 6, 8, 10};
       int index = Arrays.asList(array1).indexOf(8);
       System.out.println("Found element at location at index:"+index);
   }
}

Output:

Found element at location at index:3

Time complexity:O(n)

binarySearch() method 

In Java, you can use the binarySearch method of the Arrays class to perform a binary search on a sorted array and find the index of a particular element. It returns the index of the element the array, or a negative number if the element is not found.

Implementation:

import java.util.Arrays;
public class Main {
   public static void main(String[] args) {
       char arr[] = {1,25, 67,89};
       
       char search = 25;
       
       int result = Arrays.binarySearch(arr, search);
       
       System.out.println("Element is at index: " + result);
   }
}

Output:

Element is at index: 1

Time complexity: O(log n)

Also see, Swap Function in Java

Code to Demonstrate Java Array Indexof Method

import java.util.ArrayList;
public class IndexOfEx {
public static void main(String[] args) {

ArrayList<Integer> ar = new ArrayList<Integer>(4);


/* initializing values */
ar.add(10);
ar.add(20);
ar.add(30);
ar.add(40);


System.out.print("The ArrayList contains : \n");


for (Integer x : ar) {
System.out.print(x);
System.out.print(" ");
}


/* Java Array indexOf() method to find index of 40 */
int pos = ar.indexOf(40);

System.out.println("\nThe element 40 is at index : " + pos);
}

}
You can also try this code with Online Java Compiler
Run Code


Output

The ArrayList contains : 
10 20 30 40 
The element 40 is at index : 3
You can also try this code with Online Java Compiler
Run Code

Try it on online java compiler.

Frequently Asked Questions

What is the Java array Indexof method?

We create an array of integers and then use Arrays.asList() to convert it into a list so that we can access the indexOf() function. The indexOf() method of ArrayList returns -1 if the element is not present in this list or the index of the first instance of the element in the list.

What is the application of the Java array Indexof method?

The indexof functions are most helpful for finding the last or first occurrence of events, such as the last time the number 1 appears in a die throw or the first time any letter appears in a name.

Can you use indexOf on an array in Java?

Java doesn't have an indexOf() method for arrays, but it does have one for ArrayLists, which returns the index of the specified element. We first create an array of integers and then use Arrays.asList() to convert it into a list so that we can access the indexOf() function.

How to display array index in Java?

The three methods to determine the index of an element in Java are linear search which follows the process of iterating and comparing, and indexOf, which is a built-in method. And binary search, which is a sorted array search.

Conclusion

In this article, we have discussed Java Array indexof method in detail. We have also seen an example to get a clear understanding of this topic.

If you want to learn more, check out our articles on Java TokensWhy To Use Web2py?Postbacks and Internationalization in web2pyThird Party Modules In Web2py, and Java Verify.

Check out the following problems - 

 

Also, check out these exciting courses from coding ninjas to expand your knowledge, Coding CourseCode StudioInterview ExperienceGuided PathInterview ProblemsTest SeriesLibrary, and Resources

Happy Coding!

Live masterclass