. Advertisement .
..3..
. Advertisement .
..4..
Want to learn how to receive an array’s index by utilizing array indexOf Java? Check out this tutorial with many examples to know how to apply this method.
The JavaScript method array indexOf()
gives back the first index in the array where the given item can be found or gives a -1 value if it cannot.
The indexOf()
approach searches the array for the supplied item to return a specific item’s location to the caller code. If there isn’t a specified start position, the search will start at the supplied location and continue until it reaches the end of the array.
Get An Element’s Index In An Integer Array In Java
Java doesn’t have an indexOf()
function for arrays, but an ArrayList has one that provides the index of the supplied element. You must first generate an Integer array and use Arrays.asList()
to convert the array into a list in order to use indexOf()
.
As you can see, we utilize the wrapper class Integer rather than a simple int since asList()
will only accept wrapper classes. Still, they deliver the outcome as primitive data. You can see the following example, where the element, which in this case is 8, is specified to indexOf()
to obtain its index.
The output of the getIndex function is of type int.
import java.util.Arrays;
public class ArrayIndexOf {
public static void main (String[] args) {
Integer [] array1 = {2, 4, 6, 8, 10};
int getIndex = Arrays.asList(array1).indexOf(8);
System.out.println(“8 is located at “+getIndex+” index”);
}
}
Output:
8 is located at 3 index
Get An Array Element’s Index In Java Utilizing Java 8 Stream API
To remove specific elements from the array and determine their positions, we can utilize the Stream API. A primitive int can use the Stream operations such as range and filter thanks to the IntStream interface.
IntStream’s range()
approach will return all of the array’s elements from beginning to end. Now we employ filter()
, which accepts a predicate as its parameter.
How to determine whether the array’s currently selected element matches the elementToFind? Here, you utilize the predicate i -> elementToFind == array1[i]
, where I is the value obtained from elementToFind == array1[i]
and range()
is the criterion.
If the criteria is not met, orElse()
gives -1
and findFirst()
gives back the first element.
import java.util.stream.IntStream;
public class ArrayIndexOf {
public static void main (String[] args) {
int [] array1 = {1, 3, 5, 7, 9};
int elementToFind = 3;
int indexOfElement = IntStream.range(0, array1.length).
filter( i -> elementToFind == array1[i]).
findFirst().orElse(-1);
System.out.println(“Index of ” + elementToFind + “is ” + indexOfElement);
}
}
Output:
Index of 3 is 1
Get An Array Element’s Index In Java Utilizing Arrayutils.Indexof()
The ArrayUtils class from the Apache Commons Library is used in the below example. To import the library functions into our project, you use the following dependencies.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.11</version>
</dependency>
We utilize the ArrayUtils class’s indexOf()
method to determine the array’s index. The indexOf()
function supports two parameters: the array and the element for which you wish to get the index.
import org.apache.commons.lang3.ArrayUtils;
public class ArrayIndexOf {
public static void main (String[] args) {
int [] array1 = {1, 3, 5, 7, 9};
int elementToFind = 9;
int indexOfElement = ArrayUtils.indexOf(array1, elementToFind);
System.out.println(“Index of ” + elementToFind + “is ” + indexOfElement);
}
}
Output:
Index of 9 is 4
The Bottom Line
Above is every information you need about the array indexOf Java. Knowing how to use this function in Java is one of the most important fundamental abilities a programmer can have.Suppose you want to gain more skills array-related in Java; visit our site. We provide many detailed tutorials, such as how to sort Java arrays or convert objects’ arrays to a map, for you to check out.
Leave a comment