Java's Array indexOf()
The Java Array class doesn't have an indexOf() method. However, the Arrays utility class provides a convenient way to search for elements within an array.
IndexOf for Unsorted Non-Primitive Arrays
To find the index of an object within an unsorted array that consists of non-primitive elements, use:
java.util.Arrays.asList(theArray).indexOf(o)
IndexOf for Sorted Arrays
If the array is sorted, utilize binary search for improved performance:
java.util.Arrays.binarySearch(theArray, o)
IndexOf for Unsorted Primitive Arrays
For unsorted primitive arrays, consider alternative solutions:
The above is the detailed content of How to Find the Index of an Element in a Java Array?. For more information, please follow other related articles on the PHP Chinese website!