java – Arrays. asList(). Contains() gives the wrong result

code:

private static final int[] GOOGLE_DIRECTION_ID_FOR_MATCH = { 11,12,13,14,15 };
Log.e(TAG,"index : "
                        + Arrays.asList(GOOGLE_DIRECTION_ID_FOR_MATCH).indexOf(11));

Log.e(TAG,"contains : "
                        + Arrays.asList(GOOGLE_DIRECTION_ID_FOR_MATCH)
                                .contains(11));

Above statement log:

index : -1
contains : false

I've also tried all other values

Solution

Arrays #aslist returns a generic type

One solution is to change the array to integer instead of int:

private static final Integer[] GOOGLE_DIRECTION_ID_FOR_MATCH = { 11,15 };

I found this very useful link

Arrays#aslist returns list < int [] > instead of list < integer > (what do you want - recall, there is no such thing as list < int >) In addition, it's best to mention that if int is boxed as integer, be careful Because for example: integer (11)= eleven

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
分享
二维码
< <上一篇
下一篇>>