【问题标题】:In Java, how should I declare the return type if a recursive method can return mixed values?在Java中,如果递归方法可以返回混合值,我应该如何声明返回类型?
【发布时间】:2012-12-21 12:38:31
【问题描述】:

我有以下方法定义,旨在搜索给定键的 JSON 对象并返回 JSONObject 或该键的字符串值。为了确保它搜索 JSON 对象的每个级别,我将其设为递归,但仅在可以返回更深的 JSONObject 的情况下。编译器抱怨这必须返回一个 Object,因为我已经声明了该返回类型。美好的。在两种情况下,我返回一个对象,但我认为它的问题是在某些情况下它不会返回任何东西。如果我添加最终的返回 false 或其他内容,它将通过编译器检查,但对该方法的调用将始终(最终)返回 false,使其无用。我不习惯像 Java 这样的严格类型的语言,所以我以前没有遇到过类似的问题。任何指针将不胜感激。

public Object find(String contentId, JSONObject node) {
    JSONObject currentNode = (node != null) ? node : this.txtContent;
    Iterator<?> nodeKeys = currentNode.keys();

    while ( nodeKeys.hasNext() ){

        try {
            String key = (String) nodeKeys.next();

            if (key.equals(contentId)) {
                if (currentNode.get(key) instanceof JSONObject) {
                    return currentNode.getJSONObject(key);
                } else {
                    return currentNode.getString(key);
                }
            } else if (currentNode.get(key) instanceof JSONObject) {
                find(contentId, currentNode.getJSONObject(key));
            }
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }
}

【问题讨论】:

  • 为什么方法最终总是返回false? (请注意,返回null 通常比在此处返回false 更合适......)为什么不对递归调用find 返回的值做任何事情?
  • 它总是返回 false,因为我没有对 find() 的返回值做任何事情,正如你所指出的。

标签: java android json return type-conversion


【解决方案1】:

添加返回空值;在while循环下面

【讨论】:

    【解决方案2】:

    这个方法只有在不断地点击 else if 时才会一直返回 false,直到 while 循环条件结束并且你得到你的“return false”。

    而不是 return false 有 return null;在最后和调用方法中检查返回的对象是否为空,这意味着什么都没有找到

    【讨论】:

      【解决方案3】:

      让我们看看,你应该使用 find 调用返回的值,如果没有找到则返回 null:

      public Object find(String contentId, JSONObject node) {
          JSONObject currentNode = (node != null) ? node : this.txtContent;
          Iterator<?> nodeKeys = currentNode.keys();
      
          while ( nodeKeys.hasNext() ){
      
              try {
                  String key = (String) nodeKeys.next();
      
                  if (key.equals(contentId)) {
                      if (currentNode.get(key) instanceof JSONObject) {
                          return currentNode.getJSONObject(key);
                      } else {
                          return currentNode.getString(key);
                      }
                  } else if (currentNode.get(key) instanceof JSONObject) {
                      Object foundObj = find(contentId, currentNode.getJSONObject(key));
                      if (foundObj!=null) {
                          return foundObj;
                      }
                  }
              } catch (JSONException e) {
                  throw new RuntimeException(e);
              }
          }
          return null;
      }
      

      【讨论】:

      • 我明白了。我通常会返回对 find 的调用(例如 return find()),但这似乎在这种情况下会导致不同的返回类型问题。我想这就是让我失望的原因。感谢您花时间帮助我。
      • 没问题,我以前也遇到过这种问题。
      【解决方案4】:

      Either 类是为这种情况而设计的。见the documentation here

      用法:

      Either<OneResultType,OtherResultType> result;
      

      这避免了昂贵的 instanceOf 检查。如果找不到对象,则返回null

      【讨论】:

      • 我不知道那堂课。我会仔细看看的。谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-16
      • 2013-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多