如果有任何其他简单的方法来优化/实现下面的代码,请帮助我。像home、bill、reg这样的变量可以是空字符串(“”),也可以是空字符串(“”),这取决于场景。
我的代码
public void helloWorld()
{
List items=new ArrayList();
items.add("1"); // dummy list
String home=""; // can be null or empty
String bill=null; // can be null or empty
String reg=""; // can be null or empty
if(!items.isEmpty())
{
System.out.println("List is not null");
if(home==null&&bill==null&®==null)
{
System.out.println("home is null");
System.out.println("bill is null");
System.out.println("reg is null");
}
if(home==null&&bill==null&®!=null)
{
System.out.println("home is null");
System.out.println("bill is null");
System.out.println("reg is not null");
}
if(home==null&&bill!=null&®==null)
{
System.out.println("home is null");
System.out.println("bill is not null");
System.out.println("reg is null");
}
if(home==null&&bill!=null&®!=null)
{
System.out.println("home is null");
System.out.println("bill is not null");
System.out.println("reg is not null");
}
if(home!=null&&bill==null&®==null)
{
System.out.println("home is not null");
System.out.println("bill is null");
System.out.println("reg is null");
}
if(home!=null&&bill==null&®!=null)
{
System.out.println("home is not null");
System.out.println("bill is null");
System.out.println("reg is not null");
}
if(home!=null&&bill!=null&®==null)
{
System.out.println("home is not null");
System.out.println("bill is not null");
System.out.println("reg is null");
}
if(home!=null&&bill!=null&®!=null)
{
System.out.println("home is not null");
System.out.println("bill is not null");
System.out.println("reg is not null");
}
}
else
{
System.out.println("List is null");
}
}请给我建议。提前谢谢。
发布于 2015-07-09 15:32:47
只需分别检查home、bill和reg,因为它们彼此之间没有依赖关系。
public void helloWorld()
{
List items=new ArrayList();
items.add("1"); // dummy list
String home=""; // can be null or empty
String bill=null; // can be null or empty
String reg=""; // can be null or empty
if(!items.isEmpty())
{
System.out.println("List is not null");
System.out.println( home == null ? "home is null" : "home is not null" );
System.out.println( bill == null ? "bill is null" : "bill is not null" );
System.out.println( reg == null ? "reg is null" : "reg is not null" );
}
else
{
System.out.println("List is null");
}
}发布于 2015-07-09 15:33:08
按照建议,只需检查每个变量,而不是列出所有排列。
if(home==null)
{
System.out.println("home is null");
}
else
{
System.out.println("home is not null");
}
if (bill==null)
{
System.out.println("bill is null");
}
else
{
System.out.println("bill is not null");
}
...发布于 2022-02-03 13:15:17
public void helloWorld() {
List items = new ArrayList();
items.add("1"); // dummy list
String home = ""; // can be null or empty
String bill = null; // can be null or empty
String reg = ""; // can be null or empty
if (!items.isEmpty()) {
System.out.println("List is not null");
System.out.println("home is " + (StringUtils.isEmpty(home) ? "null" : "not null"));
System.out.println("bill is " + (StringUtils.isEmpty(bill) ? "null" : "not null"));
System.out.println("reg is " + (StringUtils.isEmpty(reg) ? "null" : "not null"));
} else {
System.out.println("List is null");
}
}StringUtils.isEmpty()函数可用于检查空值和空值。
https://stackoverflow.com/questions/31321818
复制相似问题