简体   繁体   English

Java堆栈溢出错误与递归

[英]Java stack overflow error with recursion

I have this code that keeps giving me stack overflow errors and I don't know how to fix it (i'm new to recursion). 我有这段代码一直在给我堆栈溢出错误,并且我不知道如何解决它(我是递归的新手)。 what is wrong with my code? 我的代码有什么问题?

The

        process(a, sum, row++, column);

unconditionally calls the function with exactly the same arguments due to the use of post -increment (hat tip to @fge for spotting this). 由于使用了post -increment(使用@fge提示),无条件地调用具有完全相同参数的函数。 This immediately leads to infinite recursion. 这立即导致无限递归。

Once you fix this, you'll run into another problem: you are checking that row equals a.length - 1 , and your code can make it exceed a.length - 1 . 解决此问题后,您将遇到另一个问题:您正在检查该row 等于 a.length - 1 ,并且您的代码可以使其超过 a.length - 1

        process(a, sum, row++, column);   // increment #1
        process(a, sum, row++, column--); // increment #2

This looks wrong: 这看起来是错误的:

process(a, sum, row++, column);
process(a, sum, row++, column--);

Note that the post-increment and post-decrement operators increment or decrement a variable, and then return the old value of the variable. 请注意,后递增和后递减运算符会递增或递减变量,然后返回该变量的旧值 So, in the first of these two lines, you are calling the method process with the exact same values as the original call. 因此,在这两行的第一行中,您要使用与原始调用完全相同的值来调用方法process That will then call the method again with the same values, etc., until the call stack overflows. 然后,将使用相同的值等再次调用该方法,直到调用堆栈溢出为止。

This line process(a, sum, row++, column); 此行process(a, sum, row++, column); calls process(a, sum, row, column) and then it increment row! 调用process(a, sum, row, column)then递增行!

Same function with same parameters is called again and again therefore it overflow stack. 具有相同参数的相同函数会被反复调用,因此会溢出堆栈。

This should do the trick : process(a, sum, ++row, column); 这应该可以解决问题: process(a, sum, ++row, column); It firsts increment it and then pass it as argument. 它首先将其递增,然后将其作为参数传递。 But then your row will be incremented. 但随后您的行将增加。 If you do not want to do it, simply use : process(a, sum, row + 1, column); 如果您不想这样做,只需使用: process(a, sum, row + 1, column); , it call process function with incremented row meanwhile does not change your row in method it is called from. ,它以递增的行调用流程函数,同时不会在调用它的方法中更改您的行。

You call: 您致电:

process(a, sum, row++, column);

But you use the postfix ++ operator; 但是您使用postfix ++运算符; as a result, the row value as an argument to this function will not change on call --> infinite recursion if row is not exactly equal to a.length - 1 . 结果,如果row不完全等于a.length - 1在调用 ->无限递归时,作为此函数的参数的row将不会更改

Call: 呼叫:

process(a, sum, row + 1, column);

Also, check that in the following line you actually meant to be calling: 另外,请在以下代码中检查您实际上要拨打的电话:

process(a, sum, row + 2, column);

and not: 并不是:

process(a, sum, row + 2, column - 1);

or even: 甚至:

process(a, sum, row + 1, column - 1);

Given your confused use of postfix increments/decrement operators, it is hard to tell what you mean to do... 考虑到您对后缀递增/递减运算符的混淆使用,很难说出您的意思...

Hint: in order to avoid that kind of errors in the future, make your method parameters, row and column , final ; 提示:为了避免将来发生此类错误,请将您的方法参数, rowcolumnfinal you will not be able to modify them in the method's body... 您将无法在方法的主体中对其进行修改...

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

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