Java 为什么我不能在这个方法中更改布尔变量的值

Java 为什么我不能在这个方法中更改布尔变量的值,java,algorithm,Java,Algorithm,我试着输入第一个输入,ret为真,但再次检查,ret的值仍然为假。我知道这可能与传递值有关,我尝试传递一个布尔对象,使用Boolean.FALSE或Boolean.TRUE,但没有成功。我传递了一个带有1个参数的布尔数组,这很有效 我的问题现在或多或少是一个概念性的问题,为了提高我的理解,我想知道为什么布尔运算不能按计划工作。这是一个传递值问题,还是其他问题?我必须承认,在某种程度上,我认为使用布尔vs布尔可能是解决办法 我期待您的帮助。因为这是Java,所以不能通过引用传递参数 相反,只需从方

我试着输入第一个输入,ret为真,但再次检查,ret的值仍然为假。我知道这可能与传递值有关,我尝试传递一个布尔对象,使用Boolean.FALSE或Boolean.TRUE,但没有成功。我传递了一个带有1个参数的布尔数组,这很有效

我的问题现在或多或少是一个概念性的问题,为了提高我的理解,我想知道为什么布尔运算不能按计划工作。这是一个传递值问题,还是其他问题?我必须承认,在某种程度上,我认为使用布尔vs布尔可能是解决办法


我期待您的帮助。

因为这是Java,所以不能通过引用传递参数

相反,只需从方法返回值

   public static void isMatchHelper(String input, String pattern, boolean ret){
        if(pattern.length() == 0 && input.length() == 0){
            ret =  true;
        }
        else if(pattern.length() == 0){
            ret =  false;
        }
        else if(input.length() == 0){
            ret =  true;
        }
        if(pattern.length() == 0 || input.length() == 0){
                return;
        }
        else if(pattern.charAt(0) == input.charAt(0)){
                isMatchHelper(input.substring(1), pattern.substring(1), ret);
        }
        if(pattern.charAt(0) == '.'){
            isMatchHelper(input, pattern.substring(1), ret);
        }
        if(pattern.charAt(0) == '*'){
            if(pattern.length() > 1){
                int countMatches = 0;
                char compareWith = pattern.charAt(countMatches + 1);
                while( countMatches != input.length() && input.charAt(countMatches) == compareWith){
                    countMatches++;
                }
                isMatchHelper(input.substring(countMatches), pattern.substring(2), ret);
            }
            else{
                ret =  true;
            }
        }
        if(pattern.charAt(0) != input.charAt(0)){
            isMatchHelper(input, pattern.substring(1), ret);
        }

    }
需要明确的是,由于函数是递归的,因此可以像更改非递归调用一样更改递归调用:

这条线

   public static boolean isMatchHelper(String input, String pattern) {
       boolean ret;
       .... // Rest of your code
       .... // Replace all return statements with return ret;
      return ret;
   }
变成这行:

isMatchHelper(input, pattern.substring(1), ret);

因为这是Java,所以不能通过引用传递参数

相反,只需从方法返回值

   public static void isMatchHelper(String input, String pattern, boolean ret){
        if(pattern.length() == 0 && input.length() == 0){
            ret =  true;
        }
        else if(pattern.length() == 0){
            ret =  false;
        }
        else if(input.length() == 0){
            ret =  true;
        }
        if(pattern.length() == 0 || input.length() == 0){
                return;
        }
        else if(pattern.charAt(0) == input.charAt(0)){
                isMatchHelper(input.substring(1), pattern.substring(1), ret);
        }
        if(pattern.charAt(0) == '.'){
            isMatchHelper(input, pattern.substring(1), ret);
        }
        if(pattern.charAt(0) == '*'){
            if(pattern.length() > 1){
                int countMatches = 0;
                char compareWith = pattern.charAt(countMatches + 1);
                while( countMatches != input.length() && input.charAt(countMatches) == compareWith){
                    countMatches++;
                }
                isMatchHelper(input.substring(countMatches), pattern.substring(2), ret);
            }
            else{
                ret =  true;
            }
        }
        if(pattern.charAt(0) != input.charAt(0)){
            isMatchHelper(input, pattern.substring(1), ret);
        }

    }
需要明确的是,由于函数是递归的,因此可以像更改非递归调用一样更改递归调用:

这条线

   public static boolean isMatchHelper(String input, String pattern) {
       boolean ret;
       .... // Rest of your code
       .... // Replace all return statements with return ret;
      return ret;
   }
变成这行:

isMatchHelper(input, pattern.substring(1), ret);

我认为您不能重写传递到函数中的参数。
bool
是原始的,因此会创建参数值的副本。我猜
java.lang.Boolean
也不会有帮助,因为它是不可变的,最好的办法是改变编写函数的方式,使用返回值而不是parametr@Puru--,是的,原语包装是不可变的。我认为您不能重写传递到函数中的参数。
bool
是原语,因此会创建参数值的副本。我猜
java.lang.Boolean
也不会有帮助,因为它是不可变的,最好的办法是改变编写函数的方式,使用返回值而不是parametr@Puru--,是的,原始包装是不可变的。这是一个递归函数。@ShahrukhKhan,您的意思是不能更改递归函数的返回类型吗?@ShahrukhKhan,我刚刚更新了答案,以便更具体地处理递归调用。这是一个递归函数。@ShahrukhKhan,您的意思是不能更改递归函数的返回类型吗?@ShahrukhKhan,我刚刚更新了答案,以便更具体地处理递归调用。