Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试递归使用StringBuilder,收到一个错误,指示它对类无效_Java_Stringbuilder - Fatal编程技术网

Java 尝试递归使用StringBuilder,收到一个错误,指示它对类无效

Java 尝试递归使用StringBuilder,收到一个错误,指示它对类无效,java,stringbuilder,Java,Stringbuilder,编辑:我应该提到,这是针对一个特别需要递归解决方案的赋值 我试图递归地检查一个句子是否是一个“严格”的回文,这意味着它尊重空格——“在我看到厄尔巴岛之前,我是能干的!”——或者是一个“普通”的回文,这意味着空格被忽略——“一个人,一个计划,一条运河,巴拿马!” 如果我尝试仅使用字符串值运行此程序,则会出现StackOverflow错误。StringBuilder是可变的,因此我试图找到一种方法来替代它,但我找不到任何递归使用StringBuilder的示例(我假设,因为这样做没有意义,但我不知道

编辑:我应该提到,这是针对一个特别需要递归解决方案的赋值

我试图递归地检查一个句子是否是一个“严格”的回文,这意味着它尊重空格——“在我看到厄尔巴岛之前,我是能干的!”——或者是一个“普通”的回文,这意味着空格被忽略——“一个人,一个计划,一条运河,巴拿马!”

如果我尝试仅使用字符串值运行此程序,则会出现StackOverflow错误。StringBuilder是可变的,因此我试图找到一种方法来替代它,但我找不到任何递归使用StringBuilder的示例(我假设,因为这样做没有意义,但我不知道为什么)

代码如下:

public static boolean isPalindrome(String userPal){


    if(userPal == null){
        return false;
    }
    else if( userPal.length() == 1 ){
        return true;
    }
    StringBuilder testAgainst = new StringBuilder();

    stringReversed(testAgainst);
    // String testAgainst = stringReversed(userPal);

    return userPal.equals( testAgainst );
}


public static StringBuilder stringReversed(StringBuilder toReverse){
    StringBuilder reversed = new StringBuilder(toReverse);

    if(toReverse == null){
        return null;
    }
    else if(toReverse.length() <= 1){
        return reversed;
    }

    System.out.println("This is the reverse string as it progresses: " + reversed);

    // return stringReversed( reversed.substring(1)+ reversed.charAt(0) );
    return stringReversed( reversed.substring(1, reversed.length() - 1 ) ); 

}
publicstaticbooleanispalindrome(字符串userPal){
if(userPal==null){
返回false;
}
else if(userPal.length()==1){
返回true;
}
StringBuilder testAgainst=新的StringBuilder();
反方向测试(testAgainst);
//String testAgainst=stringReversed(userPal);
返回userPal.equals(testAgainst);
}
公共静态StringBuilder stringReversed(StringBuilder toReverse){
StringBuilder反向=新StringBuilder(反向);
if(toReverse==null){
返回null;
}

否则,如果(toReverse.length()您需要初始化
StringBuilder
StringBuilder
中的值已经有了一个
reverse
方法(但它会返回一个新的
StringBuilder
)。请保存该调用的返回值或内联使用它。例如

public static boolean isPalindrome(String userPal) {
    if (userPal == null) {
        return false;
    } else if (userPal.length() < 2) {
        return true;
    }
    StringBuilder testAgainst = new StringBuilder(userPal);
    return userPal.equals(testAgainst.reverse().toString());
}
对于递归版本

public static boolean isPalindrome(String userPal) {
    if (userPal == null) {
        return false;
    } else if (userPal.length() < 2) {
        return true;
    } else if (userPal.length() == 2) {
        return userPal.charAt(0) == userPal.charAt(1);
    }
    return userPal.charAt(0) == userPal.charAt(userPal.length() - 1)
            && isPalindrome(userPal.substring(1, userPal.length() - 1));
}
publicstaticbooleanispalindrome(字符串userPal){
if(userPal==null){
返回false;
}else if(userPal.length()<2){
返回true;
}else if(userPal.length()==2){
返回userPal.charAt(0)=userPal.charAt(1);
}
返回userPal.charAt(0)=userPal.charAt(userPal.length()-1)
&&isAlindrome(userPal.substring(1,userPal.length()-1));
}

StringBuilder#subString
返回一个
字符串
。如果您尝试这样做,一个好的IDE会给您一个警告。我明白了。我正在使用Eclipse,但我得到的唯一错误是我在最初的帖子中列出的错误。这确实解释了为什么我不能以那种方式使用它…谢谢您为我澄清这一点。我知道StringBuilder有一个reverse方法,但这样使用它不是递归的……是吗?我对编程还是比较陌生,实际上这是针对类中的赋值。只需使用reverse()就很简单了方法,但该赋值特别需要递归解决方案。我并不是说听起来不受欢迎。非常感谢您花时间向我展示这一点,并解释我的错误所在。也感谢您向我展示了递归解决方案。我将在脑海中运行几次,以便理解再次感谢您!这里提供的解决方案存在
问题。
public static boolean isPalindrome(String userPal) {
    if (userPal == null) {
        return false;
    } else if (userPal.length() < 2) {
        return true;
    } else if (userPal.length() == 2) {
        return userPal.charAt(0) == userPal.charAt(1);
    }
    return userPal.charAt(0) == userPal.charAt(userPal.length() - 1)
            && isPalindrome(userPal.substring(1, userPal.length() - 1));
}