在java中尝试使用递归查找字符串的反向时出现StackOverflowerError

在java中尝试使用递归查找字符串的反向时出现StackOverflowerError,java,recursion,Java,Recursion,我试图使用递归来查找字符串的倒数,但是当我运行代码时,我得到了一个StackOverflower错误。我不熟悉递归,所以我不确定需要做什么来修复它 public static String reverse(String string) { int index = 0; if(string == null){ return " "; } else if(index < string.length()) {

我试图使用递归来查找字符串的倒数,但是当我运行代码时,我得到了一个StackOverflower错误。我不熟悉递归,所以我不确定需要做什么来修复它

public static String reverse(String string) {
        int index = 0;
        if(string == null){
            return " ";
        }
        else if(index < string.length()) {
            char a;
            a = string.charAt(index);
            index += 1;
            return a + reverse(string);
        }
        return " ";
    }
公共静态字符串反转(字符串){
int指数=0;
if(字符串==null){
返回“”;
}
else if(索引
您似乎缺少的部分是将索引传递到函数中。此外,在递归时,还需要在末尾返回当前字符。我想你想要像这样的东西

private static String reverse(String string, int index) {
    if (string != null && index < string.length()) {
        char a = string.charAt(index);
        return reverse(string, index + 1) + a;
    }
    return "";
}
public static String reverse(String string) {
    StringBuilder sb = new StringBuilder(string);
    return sb.reverse().toString();
}
当然,在真实代码中,我更喜欢
StringBuilder
之类的东西

private static String reverse(String string, int index) {
    if (string != null && index < string.length()) {
        char a = string.charAt(index);
        return reverse(string, index + 1) + a;
    }
    return "";
}
public static String reverse(String string) {
    StringBuilder sb = new StringBuilder(string);
    return sb.reverse().toString();
}

有许多问题。我已经添加了一些评论,试图提供帮助

public static String reverse(String string) {
    int index = 0;
    if(string == null){
        return " ";
    }
    /* This will always be true because index is always zero */
    else if(index < string.length()) {
        char a;
        /* This gets the character at position zero */
        a = string.charAt(index);
        /* This increments zero by 1 */
        index += 1;
        /* This takes the character and then calls reverse again (the recursion.
           The problem is that you are passing the full string in and so every time
           you recurse, index will be zero again and the string will be exactly the same length.
           So no exit criterion will ever be triggered. 
           As such this is an infinite recursion. */
        return a + reverse(string);
    }
    return " ";
}
公共静态字符串反转(字符串){
int指数=0;
if(字符串==null){
返回“”;
}
/*这总是正确的,因为索引总是零*/
else if(索引
我建议考虑以下几点:

  • 在最后一个递归调用中,即当您开始通过每个递归调用“弹出”时,您希望触发什么
  • 如果您决定在每次递归调用中通过删除第一个字符来缩短字符串,那么最后一次调用将是一个空字符串
  • 另一方面,如果您决定最后一次调用将是索引变量等于字符串长度,那么您将需要考虑传递一个额外的参数(索引),并在每次递归调用中增加一个。
这不是递归的工作方式,因为您只是一次又一次地传递相同的字符串。您可以使用递归,但有两种方法可以解决您的问题

  • 获取最后一个字符,并使用不包含最后一个字符的字符串调用该方法,如下所示:

    public String reverse(final String string) {
        if (string != null && !string.isEmpty()) {
            final int length = string.length();
            final char character = string.charAt(length - 1));  
            final String result = reverse(string.substring(0, length - 2));
            if (result != null)
                return String.valueOf(character) + result;
            return String.valueOf(character);
        }
        return null;
    }
    
  • 我不应该说我没有测试过这个,但重点是我正在更改传递的字符串,并且有一种机制来检测何时停止调用我自己的方法


    第二种方法是在不使用递归的情况下执行此操作,因为您可以使用一些for循环之类的方法来完成此操作。但是为了便于学习,请选中1:p

    您正在初始化变量”索引和“每次调用递归方法时。初始化方法块之外的所有变量”。 试试这个功能

    public static String reverse(String str){
    
        if(str.length()<=0||str.length()==1)
            return str;
        return reverse(str.substring(1))+str.charAt(0);
    }
    
    publicstaticstringreverse(stringstr){
    if(str.length()