【问题标题】:pass string parameter to a method by object itself instead of variable name (java)通过对象本身而不是变量名将字符串参数传递给方法(java)
【发布时间】:2016-02-15 18:47:10
【问题描述】:

参数传递机制让我很困惑。我已经阅读了大量关于此的文章,例如 Is Java "pass-by-reference" or "pass-by-value"?。我知道他们通过价值传递。但是,他们并没有谈论一种情况:经过对象本身。

我在解决 leetcode 问题时遇到了这个问题。

生成括号

给定 n 对括号,编写一个函数来生成格式正确的括号的所有组合。

例如,给定 n = 3,解集是:

“((()))”、“(()())”、“(())()”、“()(())”、“()()()”

还有一个递归代码

public class Solution {
public List<String> generateParenthesis(int n) {
    List<String> result = new ArrayList<String>();
    String str = new String("");
    helper(result, str, 0, 0, n);
    return result;
}

public void helper(List<String> result, String str, int left, int right, int n){
    if(left == n && right == n){
        result.add(str);
        return;
    }

    if(left < right){
        return;
    }

    if(left < n){
        helper(result, str + "(", left + 1, right, n);
    }

    if(right < n){
        helper(result, str + ")", left, right + 1, n);
    } 
}    
}

我很难理解代码的作用,尤其是:

    if(left < n){
        helper(result, str + "(", left + 1, right, n);
    }

    if(right < n){
        helper(result, str + ")", left, right + 1, n);
    } 

helper方法的第二个参数是通过字符串而不是字符串的变量名传递的,这种情况会发生什么?我想这可能是阻碍我理解代码的部分。谁能告诉我为什么这段代码有效?我确实花了很多时间阅读 Java 中的参数传递机制和递归,但这仍然让我感到困惑。

非常感谢。

【问题讨论】:

  • 请详细说明问题?
  • 表达式str + "(" 的求值结果是对String 对象的引用,该对象是str 引用的String 和String "(" 的串联。因此,您仍然按值传递对 String 类型对象的引用。无论您使用的是变量还是表达式都不会改变任何内容。
  • 既不是,也不是。您将引用的副本传递给对象。所以内存中有一些字符串。当您调用方法时,会动态生成对它的新引用。

标签: java recursion parameter-passing


【解决方案1】:

Java 中的基元是按值传递的,但对象传递引用的副本。同样显示此代码块的最后四行,3/4 是花括号。你到底想了解什么?

所以这段代码基本上提供了一个字符串列表,使用递归方法。我不知道您对递归了解多少,但它基本上是在同一个方法中调用该方法。一开始可能会让人困惑,但如果你能理解它,你就可以编写漂亮而简短的代码。基本上这是在列表结果中添加一个字符串。 left 和 right 是当前 String 中左括号和右括号的数量。我对代码进行了注释,也许可以帮助您更好地理解。

    public class Solution {
public List<String> generateParenthesis(int n) {
    List<String> result = new ArrayList<String>(); //The list that gets returned at the end of generateParenthesis method.
    String str = new String(""); //Initialized string that contains nothing.
    helper(result, str, 0, 0, n); //First call of helper method.
    return result; //Final result gets returned to caller.
}

public void helper(List<String> result, String str, int left, int right, int n){
    //If number of left parenthesis and number of right parenthesis is the number of parenthesis total we need, which in your example is 3 of each, add the string to the List result and return to the last caller.
    if(left == n && right == n){
        result.add(str);
        return;
    }

//If the number of left parenthesis is less than the number of right parenthesis, return to the last call of method.
        if(left < right){
            return;
        }
    //If the number of left parenthesis is less than the number of total required, add a left parenthesis to String str and incremement number of left parenthesis by one.
        if(left < n){
            helper(result, str + "(", left + 1, right, n); //call helper via recursion and make the new value of str = str+"(" and increment number of left parenthesis by 1.
        }



  //If number of right parenthesis is less than the number of total parenthesis required, add a right facing parenthesis to String str and increment number of right parenthesis by one.
        if(right < n){
            helper(result, str + ")", left, right + 1, n);
        } 
    }  

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 2021-06-21
    相关资源
    最近更新 更多