java – Arrays. Contains (int) error

May I ask why the following output is false?

import java.util.Arrays;


public class Test2 {

    public static void main(String[] args) {
        new Test2();
    }

    private final int[] VOWEL_POS = {0,4,8,14,20};

    Test2(){
        if(Arrays.asList(VOWEL_POS).contains(0)){
            System.out.print("TRUE");
        }else{
            System.out.print("FALSE");
        }

    }

}

thank you!

Solution

The aslist method here returns a list < int [] >, which is not what you expect

The reason is that you can't have list < int > To achieve what you want, create an integer - integer [] array

Apache commons Lang has such arrayutils:

if(Arrays.asList(ArrayUtils.toObject(VOWEL_POS)).contains(0))

Or initially make the array integer [], so that no conversion is required

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