Jamie Jamie - 1 month ago 6
Java Question

Where is Java's Array indexOf?

I must be missing something very obvious, but I've searched all over and can't find this method.

Answer

There are a couple of ways to accomplish this using the Arrays utility class.

If the array is not sorted:

java.util.Arrays.asList(theArray).indexOf(o)

If the array is sorted, you can make use of a binary search for performance:

java.util.Arrays.binarySearch(theArray, o)
Comments