簡體   English   中英

Arrays 類型的方法 allBoolean(Boolean[]) 未定義

[英]The method allBoolean(Boolean[]) is undefined for the type Arrays

我需要一個我正在編寫的程序,一種允許我檢查任何數組是否完全由真、假或兩者混合組成的方法。 我在這里提出了代碼,但顯然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, true, true, true, true, true, true};
    public static Boolean[] examplearray2 = {true, true, true, true, true, true, true, true};
    public static Boolean[] examplearray3 = {true, true, true, true, true, true, true, 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM