我正在尝试从函数中接收多个参数,并且我正在检查其中至少有一个参数是非null还是空。
现在我正在做这样的事情。
void foo(String a, String b, String c, String d, ... other strings){
//make sure at least one of the inputs are not null.
if(a!=null || b!=null || c!=null || d!=null ... more strings){
//do something with the string
}
}
所以输入可以是foo(null, null, null, "hey");
但它不能是foo(null, null, null, null);
我的问题是有更好的方法来做到这一点,而不是继续添加到if语句。我现在正在消隐....谢谢
答案 0 :(得分:4)
使用varags
public static boolean atLeastOneEmpty(String firstString, String... strings){
if(firstString == null || firstString.isEmpty())
return true;
for(String str : strings){
if(str == null || str.isEmpty())
return true;
}
return false;
}
如果至少有一个字符串为空,则返回true