There are a couple of ways to accomplish this using the Arrays utility class.
If the array is not sorted and is not an array of primitives:
java.util.Arrays.asList(theArray).indexOf(o)
If the array is primitives and not sorted, one should use a solution offered by one of the other answers such as Kerem Baydoğan's, Andrew McKinlay's or Mishax's. The above code will compile even if theArray is primitive (possibly emitting a warning) but you'll get totally incorrect results nonetheless.
If the array is sorted, you can make use of a binary search for performance:
java.util.Arrays.binarySearch(theArray, o)
Answer from Jeffrey Hantin on Stack OverflowThere are a couple of ways to accomplish this using the Arrays utility class.
If the array is not sorted and is not an array of primitives:
java.util.Arrays.asList(theArray).indexOf(o)
If the array is primitives and not sorted, one should use a solution offered by one of the other answers such as Kerem Baydoğan's, Andrew McKinlay's or Mishax's. The above code will compile even if theArray is primitive (possibly emitting a warning) but you'll get totally incorrect results nonetheless.
If the array is sorted, you can make use of a binary search for performance:
java.util.Arrays.binarySearch(theArray, o)
Array has no indexOf() method.
Maybe this Apache Commons Lang ArrayUtils method is what you are looking for
import org.apache.commons.lang3.ArrayUtils;
String[] colours = { "Red", "Orange", "Yellow", "Green" };
int indexOfYellow = ArrayUtils.indexOf(colours, "Yellow");
Integer[] array = {1,2,3,4,5,6};
Arrays.asList(array).indexOf(4);
Note that this solution is threadsafe because it creates a new object of type List.
Also you don't want to invoke this in a loop or something like that since you would be creating a new object every time
Another option if you are using Guava Collections is Ints.indexOf
// Perfect storm:
final int needle = 42;
final int[] haystack = [1, 2, 3, 42];
// Spoiler alert: index == 3
final int index = Ints.indexOf(haystack, needle);
This is a great choice when space, time and code reuse are at a premium. It is also very terse.
Before Java 5, Arrays.asList used to accept an Object[]. When generics and varargs were introduced into the language this was changed to
public static <T> List<T> asList(T... arr)
In your example, T cannot be int because int is a primitive type. Unfortunately, the signature matches with T equal to int[] instead, which is a reference type. The result is that you end up with a List containing an array, not a List of integers.
In Java 4, your code would not have compiled because an int[] is not an Object[]. Not compiling is preferable to producing a strange result, and in Effective Java, Josh Bloch says retrofitting asList to be a varargs method was a mistake.
Arrays.asList expect an object (or more). Your array (array) is a sole object, thus calling the method will create a list of only one object, which is your array.
Calling indexOf on your list will return -1 because 4 will never be found as your list contains the array object, not the list of datas that your array contains.
Currently the best way I have found is to typecast the array into an ArrayList, and using ArrayList's method .indexOf to get the index, for e.g:
import java.util.Arrays;
int[] arr = {2,3,4,5};
Arrays.asList(arr).indexOf(4); // returns 2Is this the best way? Or unnecessary wrapping?