通过对象本身而不是变量名(java)将字符串参数传递给方法

通过对象本身而不是变量名(java)将字符串参数传递给方法,java,recursion,parameter-passing,Java,Recursion,Parameter Passing,参数传递机制让我很困惑。我读过很多关于这方面的文章,比如。我知道他们是按价值传递的。然而,他们没有谈论一种情况:通过对象本身 我在解决leetcode问题时遇到了这个问题 生成括号 给定n对括号,编写函数以生成格式良好的括号的所有组合 例如,给定n=3,解决方案集为: “(())”、“(())”、“(())()”、“(())()”、“(())”、“(())” 还有一个递归代码 public class Solution { public List<String> generatePa

参数传递机制让我很困惑。我读过很多关于这方面的文章,比如。我知道他们是按价值传递的。然而,他们没有谈论一种情况:通过对象本身

我在解决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);
    } 
}    
}
公共类解决方案{
公共列表生成器属性(int n){
列表结果=新建ArrayList();
String str=新字符串(“”);
助手(结果,str,0,0,n);
返回结果;
}
公共void帮助器(列表结果、字符串str、int left、int right、int n){
if(左==n&&right==n){
结果:添加(str);
返回;
}
if(左<右){
返回;
}
if(左
我正在努力理解代码的作用,尤其是:

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

    if(right < n){
        helper(result, str + ")", left, right + 1, n);
    } 
if(左
helper方法的第二个参数由字符串而不是字符串的变量名传递,在这种情况下会发生什么?我想这可能是阻碍我理解代码的部分。有人能告诉我为什么这个代码有效吗?我确实花了很多时间阅读Java中的参数传递机制和递归,但它仍然让我感到困惑


非常感谢。

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

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

    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);
        } 
    }  
公共类解决方案{
公共列表生成器属性(int n){
List result=new ArrayList();//在generateParenthesis方法末尾返回的列表。
String str=new String(“”;//初始化的字符串不包含任何内容。
helper(result,str,0,0,n);//helper方法的第一次调用。
return result;//将最终结果返回给调用方。
}
公共void帮助器(列表结果、字符串str、int left、int right、int n){
//如果左括号数和右括号数是我们需要的总括号数(在您的示例中为每个括号的3个),请将字符串添加到列表结果中,并返回到最后一个调用方。
if(左==n&&right==n){
结果:添加(str);
返回;
}
//如果左括号的数目小于右括号的数目,则返回方法的最后一次调用。
if(左<右){
返回;
}
//如果左括号的数目小于所需的总数,则向字符串str添加一个左括号,并将左括号的数目增加一。
if(左
请详细说明问题?表达式
str+”(“
是对字符串对象的引用,该字符串对象是
str
引用的字符串与字符串
的串联(”
。因此,您仍在通过值传递对String类型对象的引用。无论您使用的是变量还是表达式,都不会改变任何内容。既不是,也不是。您将引用的副本传递给对象。因此内存中存在一些字符串。当您调用方法时,会动态生成对该对象的新引用。