The following code in Java return -1. I thought that it should return 3.
int[] array = {1,2,3,4,5,6};
System.out.println(Arrays.asList(array).indexOf(4));
Can you help me understand how this function works.
Thanks
The following code in Java return -1. I thought that it should return 3.
int[] array = {1,2,3,4,5,6};
System.out.println(Arrays.asList(array).indexOf(4));
Can you help me understand how this function works.
Thanks
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.
int[] array = {1,2,3,4,5,6};
Arrays.stream(array).boxed().collect(Collectors.toList()).indexOf(4);
should do what you want while keeping the original int[]
.
You get an IntStream
of it, box it into a Stream<Integer>
, collect it to a List<Integer>
and then can do an .indexOf()
search.
You've got answers as as to what is happening to your array when you use Arrays.asList()
.
Other things to consider:
Arrays.binarySearch()
int[]
to a List<Integer>
Example:
public static void main(String[] args) throws Exception {
int[] array = {1, 2, 3, 4, 5, 6};
System.out.print("Binary Search: ");
System.out.println(Arrays.binarySearch(array, 4));
int[] array2 = {5, 8, 2, 5, 3, 4, 1};
System.out.print("Manual Search: ");
System.out.println(indexOf(array2, 4));
}
public static int indexOf(int[] array, int search) {
for (int i = 0; i < array.length; i++) {
if (array[i] == search) {
return i;
}
}
return -1;
}
Results:
Binary Search: 3
Manual Search: 5
Since you are using an array of primitive type, this will not work. The array is of type int. Were you using Integer
type for example, that will work.
-1 means the index of the specified element was not found.
This is what you want to do:
Integer[] array = {1,2,3,4,5,6};
System.out.println(Arrays.asList(array).indexOf(4));
You cannot use primitive types such as int as parameters to generic classes in Java since int is not a class,you shoud use Integer instead :
Integer[] array = {1,2,3,3,4,5};
List Arraylist=Arrays.asList(array);
System.out.println(Arraylist.indexOf(1));
System.out.println(Arraylist.indexOf(4));
output:
0
4
note that Arraylist.indexOf(i)
return the index of first occurrence of the element i in list :
System.out.println(Arraylist.indexOf(3));
it will return 2
which is first occurrence of element 3 , not 3
since there is 2 elements of 3 in list.
Because when you do this with a primary data type array, it will be treated as the first element in the List.
int[] array = {1,2,3,4,5,6};
System.out.println(Arrays.asList(array));
You will get this:
[[I@474e8d67]
int[] array = {1,2,3,4,5,6};
array
is considered as an Object
int[]
is particularly Object
and not Object[]
Arrays.asList
you need zero or more Objects
Your method gets called in this way,
Arrays.asList((Object)(array));//Yes
Arrays.asList(1,2,3,4,5,6);//No
So you will have List<int[]>
and not List<Integer>
Now, you might have asked your self that if
Arrays.asList(1,2,3,4)
works than why notArrays.asList(arrayInt)
?
In Arrays.asList(1,2,3,4)
all the values are autoboxed in to the Integer
which is solely an Object
so in above case Integer[]
cad do the trick.