简体   繁体   中英

I'm trying to write a recursive static method with a helper in order to reverse a string. Why am I getting “stack overflow?”

So as the title says, I want to write a static method in Java that takes a String (like "hello!") and reverses it using a helper recursive method. However, I get stack overflow errors. My code is:

  public static String reverse2(String text){
    return reverseHelp(text, "", text.length()-1);
  }

  public static String reverseHelp(String text, String newString, int index) {
    if(index <= 0){
      return newString;
    }
    else
    {
      index = text.length()-1;
      return newString = text.charAt(index) + reverseHelp(text, newString, index - 1);
    }

  }

What I'm trying to do is append the last character to the new string. How can I get this to work?

You're setting index to text.length() - 1 every time. You're not actually taking in the value of your index (you pass it as a parameter, but then you overwrite it each time). Since you're not actually changing the length of your string, you're never going to be able to break out of the recursive function, hence a stack overflow error.

 public static String reverse2(String text){
    return reverseHelp(text, "", text.length()-1);
  }

  public static String reverseHelp(String text, String newString, int index){
    if(index < 0){
      return newString;
    }
    else
    {
      index = text.length()-1;
      return newString = text.charAt(index) + reverseHelp(text.substring(0,index), newString, index - 1);
    }
  }

2 errors, the first is reverseHelp(text.substring(0,index), newString, index - 1); you need to recurse over a smaller string or else it won't terminate.

The second is if(index < 0){ otherwise it misses the first/last character.

However there are still some code quality issues.

You don't need to assign on the last statement, and you also no longer need to pass the index through, as the string is changing.

Alternately, you could not substring, if you don't assign a new value to index each time.

ultimate, this worked out:

  public static String reverse2(String text){
    return reverseHelp(text, "", text.length()-1);
  }

  public static String reverseHelp(String text, String newString, int index){
    if(index < 0){
      return newString;
    }
    else{
     return newString = text.charAt(index) + reverseHelp(text, newString, index - 1); 
    }
  }

The reason why your solution did not work out because your methods are static but your variable String text is behaving local & therefore text.length() - 1 would give the same value each time resulting in stack overflow.

If you make your text variable as static class member & use it

OR even better if you make

int len = text.length()

as static member and use len in place of text.length() your code will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM