Why is indexOf failing to find the object?

Arrays.asList(A) returns a List<int[]>. This is because it expects an array of objects, not primitive types. Your options include:

  • use Integer[] instead of int[]
  • inline the array, and let autoboxing take care of it; Arrays.asList(3,8,2,5,1,4,7,9) will work fine
  • use Guava’s Ints.asList(int...) method to view the primitive array as a List<Integer>. (Disclosure: I contribute to Guava.)
  • use Guava’s Ints.indexOf(int[], int), which works directly on primitive arrays.

Leave a Comment