简体   繁体   English

为什么在递归java方法中出现“线程“ main”中的异常“ main” java.lang.StackOverflowError”?

[英]Why Am I getting “Exception in thread ”main“ java.lang.StackOverflowError” in recursive java method?

I'm trying to get an output that doubles the number I enter. 我正在尝试获得使输入的数字翻倍的输出。 In my example, i put 5, and want my output to be 10, 8, 6, 4, 2. But I get an error saying 在我的示例中,我输入5,并希望输出为10、8、6、4、2。但是我收到一条错误消息,说

Exception in thread "main" java.lang.StackOverflowError
    at HelloWorld.recursion(HelloWorld.java:13)
    at HelloWorld.recursion(HelloWorld.java:13)
    at HelloWorld.recursion(HelloWorld.java:13)
    at HelloWorld.recursion(HelloWorld.java:13) 

However, I've seen code similar to mine and they got it correctly, what am I doing wrong? 但是,我看过与我的代码相似的代码,他们正确地将其获取,我在做什么错呢? Why is line 13 wrong? 为什么第13行错了?

   public class HelloWorld{

     public static void main(String []args){
        System.out.println(recursion(5));
     }

     public static int recursion(int x){
         int temp = x--;
         if(x == 0){
             return 0;
         }
         else if(x > 0){
             return recursion(temp) + x*2;
         }
         return -1;
     }
}

Let's walk through each line of this when recursion(5) is called. 让我们遍历调用recursion(5)的每一行。

public static int recursion(int x){ // x is 5
     int temp = x--; // temp is 5, x is 4
     if(x == 0){
         return 0;
     }
     else if(x > 0){
         return recursion(temp) + x*2; // calls recursion(5) again
     }
     return -1;
 }

As others have mentioned above, you are using a post-decrement where you probably want a pre-decrement. 就像其他人在上面提到的那样,您正在使用后减量,而您可能希望先减量。

int x = 5, y = 5;
int a = x--; // a is 5, x is 4
int b = --y; // b is 4, y is 4

Change int temp = x--; 更改int temp = x--; to int temp = --x; int temp = --x; Otherwise, your recursion is infinite. 否则,您的递归是无限的。

public class HelloWorld{

 public static void main(String []args){
    System.out.println(recursion(5));
 }

 public static int recursion(int x){
   int recursion(int x){
     if(x == 0){
         return 0;
     }
     else if(x> 0){
          System.out.println(x*2);
          recursion(--x);
     }
 }

You are using post-decrement to assign value to int temp : 您正在使用递减后赋值给int temp

int temp = x--; is the the same as int temp = x; x--; int temp = x; x--;相同int temp = x; x--; int temp = x; x--; , so your argument for recursion in line 13 is the same as the previous function call. ,因此第13行中的recursion参数与上一个函数调用相同。 As a result of it, your recursion function gets into an infinite loop. 结果,您的recursion函数陷入了无限循环。

To get it to work, use pre-decrement instead, ie: 要使其正常工作,请改使用减量 ,即:

change int temp = x--; 改变int temp = x--; to int temp = --x; int temp = --x; , which is equivalent to --x; int temp = x; ,相当于--x; int temp = x; --x; int temp = x; . Now your argument in recursion(temp) is decremented by 1. 现在, recursion(temp)参数递减1。

When you call a function, its data (like arguments and local variables) is put onto what is known as the function call stack. 当您调用函数时,其数据(如参数和局部变量)将被放入所谓的函数调用堆栈中。 This stack has limited space, so if your recursion does not stop you get what is known as stack overflow. 该堆栈的空间有限,因此如果递归没有停止,您将得到所谓的堆栈溢出。 Now as for the code, others have pointed out what is wrong. 现在,对于代码,其他人指出了错误所在。 temp is not being assigned the value that you think it is. 尚未为temp分配您认为正确的值。 I would recommend adding a print statement inside the recursive function to see what values of x are actually coming in. If for example, the same value is being passed in every time then the recursion is not really making any progress and will hit the stack size limit. 我建议在递归函数中添加一条print语句,以查看x的实际值。例如,如果每次都传递相同的值,则递归实际上并没有取得任何进展,并且会影响堆栈的大小限制。 Hopefully that will help you narrow down similar problems in the future. 希望这将有助于您将来缩小类似问题的范围。 Happy debugging! 调试愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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