java - Arrays.asList(int[]) not working -


this question has answer here:

when run following code, no output printed.

int[] array = {3, 2, 5, 4};  if (arrays.aslist(array).contains(3)) {     system.out.println("the array contains 3"); } 

when pass array of primitives (int[] in case) arrays.aslist, creates list<int[]> single element - array itself. therefore contains(3) returns false. contains(array) return true.

if you'll use integer[] instead of int[], work.

integer[] array = {3, 2, 5, 4};  if (arrays.aslist(array).contains(3)) {   system.out.println("the array contains 3"); } 

a further explanation :

the signature of aslist list<t> aslist(t...). primitive can't replace generic type parameter. therefore, when pass method int[], entire int[] array replaces t , list<int[]>. on other hand, when pass integer[] method, integer replaces t , list<integer>.


Comments