I have following Java code,
int a[] = new int[] {20, 30} ;
List lis = Arrays.asList(a) ;
System.out.print(lis.contains(20));
However, output is false. Can anybody help me, why this is not giving True ?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
What you get is not a list of integers but a list of integer arrays, i.e.
List<int[]>. You can’t create collections (likeList) of primitive types.In your case, the
lis.contains(20)will create anIntegerobject with the value 20 and compare that to the int array, which clearly isn’t equal.Try changing the type of the array to
Integerand it should work: