【问题标题】:method requiring return statement outside for loop需要在 for 循环外返回语句的方法
【发布时间】:2018-12-05 22:05:50
【问题描述】:

我在这里的方法在 Eclipse 上遇到了问题。如果在名为 catalog 的数组中找到对象,我需要返回 Country 对象,如果找不到则返回 null。我试图遍历目录并这样做。但是 java 要求我在代码的 for 循环之外添加一个 return 语句。但是,当我在执行该方法时在 for 循环之外添加 return 语句时,它会完全忽略 for 循环,只返回 for 循环之外的语句。

public Country findCountry(String countryname) {
    for (int i = 0; i < catalogue.length; i++) {
        if (catalogue[i].getName() == countryname) {
            return catalogue[i];
        } else {
            return null;
        }
    }
}

编辑:在循环之前添加了 foundCountry 变量并在之后返回它。添加了一个中断,并使用 .equals() 比较字符串。获取 NullPointerException。

public Country findCountry(String countryname) {
        Country foundCountry = null;
        for (int i = 0; i < catalogue.length; i++) {
            if (catalogue[i].getName().equals(countryname)) {
                foundCountry = catalogue[i];
                break;
            }
        }
        return foundCountry;
    }

【问题讨论】:

  • 也许你的catalogue 数组是空的。或者问题可能是将字符串与 ==(使用等于)进行比较
  • 请注意,您实际上并没有在这里循环,您总是在第一次迭代时返回。如果目录长度为零怎么办?将return null: 移到循环外。

标签: java for-loop if-statement return curly-braces


【解决方案1】:

另一个使用流的版本(需要 Java 8 或更高版本)并检查 catalogue 不是 null

public Country findCountry(String countryName) {
    if (catalogue == null) {
        return null;
    }

    return Arrays.stream(catalogue)
        .filter(country -> country.getName().equals(countryName))
        .findAny()
        .orElse(null);
}

【讨论】:

    【解决方案2】:
    public Country findCountry(String countryname) {
            Country foundCountry = null;
            for (int i = 0; i < catalogue.length; i++) {
                if (catalogue[i].getName().equals(countryname)) {
                  foundCountry = catalogue[i];
                  break;
                }
            }
            return foundCountry;
        }
    

    找到后,中断循环以提高性能。 未找到时,默认返回 null。

    【讨论】:

    • 我在循环之前添加了 foundCountry 并在循环之后返回它,但是当我测试代码时我仍然收到 NullPointerException。我用来测试我的代码的类会打开一个文本文件并将国家/地区存储在目录数组中,因此数组不能为空。
    • 看起来国家名称在目录中找不到/不存在。文本文件是否与您作为 findCountry 方法的参数传递的国家相同?目录是 Country 类型的数组吗?而 getName() 获取国家/地区名称?
    【解决方案3】:

    您可以使用 null 初始化返回值,并且只有在循环中找到时才设置它:

        public Country findCountry(String countryname) {
            // initialize a Country with null
            Country foundCountry = null;
            // try to find it
            for (int i = 0; i < catalogue.length; i++) {
                if (catalogue[i].getName().equals(countryname)) {
                    // set it if found
                    foundCountry = catalogue[i];
                }
            }
            // if not found, this returns null
            return foundCountry;
        }
    

    【讨论】:

    • 使用==进行字符串比较?
    • @ShanuGupta 你说的完全正确,这是一件不应该做的事情......我会纠正它,还没有注意;-)
    【解决方案4】:

    改变

    catalogue[i].getName() == countryname
    

    catalogue[i].getName().equals(countryname)
    

    并且不要在else 部分中使用return null。当循环完成并且没有找到像这样的东西时这样做:

    public Country findCountry(String countryname) {
        for (int i = 0; i < catalogue.length; i++) {
            if (catalogue[i].getName().equals(countryname)) {
                return catalogue[i];
            }
        }
        return null;
    }
    

    请注意,它不是Null证明。

    【讨论】:

      【解决方案5】:

      删除

      else {
                  return null;
           }
      

      把它放在你的for循环之外

      return null;
      

      如果国家不是目录中的第一个元素,您的代码将返回 null。它不会遍历它。

      【讨论】:

        猜你喜欢
        • 2015-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-27
        • 1970-01-01
        • 1970-01-01
        • 2018-03-07
        • 2016-08-23
        相关资源
        最近更新 更多