据我了解,您有两种情况可以涵盖三种情况。因此,如果您的数组包含的值不同于“至少一个真、假和空”或“全为假”,那么您的问题不清楚必须返回什么值。因此,您可以做的是为第三种情况引发异常(例如,数组仅包含true
值或仅包含false
值等)
此外,不清楚您是指字符串“null”还是 null 值。没有这些信息,很难理解如何更好地提供帮助,但我认为您可以使用此代码更好地指导自己:
public class A {
public boolean isAllRecordsAreValid (String []list) {
boolean hasNull = false;
boolean hasTrue = false;
boolean hasFalse = false;
boolean allAreFalse = true;
for (int i=0;i<list.length; i++) {
if( list[i] == null || list[i].equals("null") ){
hasNull = true;
allAreFalse = false;
} else if( list[i].equals( "true" ) ){
hasTrue = true;
allAreFalse = false;
} else if( list[i].equals( "false" ) ){
hasFalse= true;
}
if( hasNull && hasTrue && hasFalse ) {
return true;
}
}
if( allAreFalse ){
return false;
}else{
throw new UnsupportedOperationException("The array is not supported");
}
}
public static void main(String[] args) {
String[] test1 = {"true", "false", null};
String[] test2 = {"true", "false", "null"};
String[] test3 = {"false", "false", "false"};
String[] test4 = {"true", "true", "true"};
A a = new A();
System.out.println( "test1: "+ a.isAllRecordsAreValid( test1 ) );
System.out.println( "test2: "+ a.isAllRecordsAreValid( test2 ) );
System.out.println( "test3: "+ a.isAllRecordsAreValid( test3 ) );
System.out.println( "test4: "+ a.isAllRecordsAreValid( test4 ) );
}
}