未为Arrays类型定义方法allBooleanBoolean []

问题描述

我需要一个我正在编写的程序,该方法允许我检查任何数组(如果数组完全由true,false或两者混合组成)。我在这里提出了代码,但显然The method allBoolean(Boolean[]) is undefined for the type Arrays

    public static void main(String[] args) {
        myClass a = new myClass();
        Arrays.allBoolean(examplearray1);
    }
    public static Boolean[] examplearray1 = {true,true,true};
    public static Boolean[] examplearray2 = {true,true};
    public static Boolean[] examplearray3 = {true,true};

public void allBoolean(Boolean[] a) {
    if (Arrays.asList(a).contains(false)) {
    if (Arrays.asList(a).contains(true)){
        System.out.println("mixed");
    }
    else {
        System.out.println("all false");
    }
    }
    else {
        System.out.println("all true");
    }
}

我真的不知道怎么了。可能只是我在编写代码时犯了一些愚蠢的错误,但是如果有人知道为什么它会向我发送错误消息,那么非常感谢帮助

解决方法

allBoolean()是您类中的方法,而不是Arrays中的方法。 Arrays.allBoolean(examplearray1);应该是a.allBoolean(examplearray1);,因为您以前做过myClass a = new myClass();

,
Arrays.asList(yourArray).contains(yourValue)

这不适用于基本数组

使用流代替,那么您就不需要这种很难理解的if-else逻辑

boolean test = Stream.of(a).anyMatch(e->e == true);
boolean test1= Stream.of(a).anyMatch(e->e == false);