Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java list.contains returning false, should be true [duplicate]

Okay, I was trying to do some conditional checks and noticed this returned false.... Something I'm missing?

int test = 1;

int[] testing= {1,3};

System.out.println(Arrays.asList(testing).contains(test) );  //false???
like image 785
Doc Holiday Avatar asked Mar 18 '14 14:03

Doc Holiday


1 Answers

Arrays.asList is a generic method where the generic type variable is used in the vararg parameter. Primitive types don't work with generics.

Therefore, after

Arrays.asList(testing)

the List returned contains a single element, an array of type int.

Your test would pass if testing was declared as an Integer[].

like image 84
Sotirios Delimanolis Avatar answered Sep 23 '22 19:09

Sotirios Delimanolis