Java array list problem

I have the following java code,

int a[] = new int[] {20,30} ;
List lis = Arrays.asList(a) ;
System.out.print(lis.contains(20));

However, the output is false Anyone can help me. Why isn't this true?

Solution

What you get is not a list of integers, but a list of integer arrays, that is, list < int [] > You cannot create a collection of basic types (such as list)

In your case, LIS Contains (20) will create an integer object with a value of 20 and compare it with the int array, which is obviously not equal

Try changing the type of the array to integer, which should work:

Integer a[] = new Integer[] {20,30} ;
List lis = Arrays.asList(a) ;
System.out.print(lis.contains(20));
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>