如何解决未为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);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。