首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java if条件代码优化

Java if条件代码优化
EN

Stack Overflow用户
提问于 2015-07-09 15:24:52
回答 4查看 94关注 0票数 0

如果有任何其他简单的方法来优化/实现下面的代码,请帮助我。像home、bill、reg这样的变量可以是空字符串(“”),也可以是空字符串(“”),这取决于场景。

我的代码

代码语言:javascript
复制
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&&reg==null) 
        {
            System.out.println("home is null");
            System.out.println("bill is null");
            System.out.println("reg is null");
        }
        if(home==null&&bill==null&&reg!=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&&reg==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&&reg!=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&&reg==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&&reg!=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&&reg==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&&reg!=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");
    }
}

请给我建议。提前谢谢。

EN

回答 4

Stack Overflow用户

发布于 2015-07-09 15:32:47

只需分别检查homebillreg,因为它们彼此之间没有依赖关系。

代码语言:javascript
复制
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");
    }
}
票数 3
EN

Stack Overflow用户

发布于 2015-07-09 15:33:08

按照建议,只需检查每个变量,而不是列出所有排列。

代码语言:javascript
复制
    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");
    }
    ...
票数 1
EN

Stack Overflow用户

发布于 2022-02-03 13:15:17

代码语言:javascript
复制
 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()函数可用于检查空值和空值。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31321818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档