How can I find an index of a certain value in a Java array of type int?
I tried using Arrays.binarySearch on my unsorted array, it only som
In case anyone is still looking for the answer-
You can use ArrayUtils.indexOf() from the [Apache Commons Library][1].
If you are using Java 8 you can also use the Strean API:
public static int indexOf(int[] array, int valueToFind) {
if (array == null) {
return -1;
}
return IntStream.range(0, array.length)
.filter(i -> valueToFind == array[i])
.findFirst()
.orElse(-1);
}
[1]: https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/ArrayUtils.html#indexOf(int[],%20int)