java 使用递归如何保持局部变量更新
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7969955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using recursion how can i keep the local variable updated
提问by user647207
Using recursion how can i keep the local variable updated till a condition is met. I have an instance below that explains why question very well. The count variable is a local variable and very time the method goes through the count is set to 0. I cannot move the count outside the method it has to be local variable not static or anything else.. The desired output should be (3 6)
使用递归如何保持局部变量更新直到满足条件。我在下面有一个实例,可以很好地解释为什么提出问题。计数变量是一个局部变量,并且方法通过计数的时间被设置为 0。我不能将计数移到方法之外,它必须是局部变量而不是静态或其他任何东西.. 所需的输出应该是 (3 6 )
public static int returnInt(int b) {
int count = 0;
if (b == 6) {
System.out.println(count + " " + b);
}
b += 2;
count++;
return returnInt(b);
}
回答by Matt Fenwick
Pass count
as an additional parameter to the method:
通过count
作为附加参数的方法:
public static int returnInt(int b, int count) {
... do some stuff to b, print something, check whether end-condition has been met ...
return returnInt(b, count + 1);
}
Then, call returnInt
with an extra argument of 0
to start things off.
然后,returnInt
使用额外的参数调用0
来开始工作。
回答by AlexR
You cannot. Local variable is local by definition. It exists only in scope of current execution of the method.
你不能。局部变量根据定义是局部的。它仅存在于方法的当前执行范围内。
BUT you have the following solutions.
但是您有以下解决方案。
The best one is to pass it as a parameter of your method (exactly as you do with other parameter b
.
最好的方法是将它作为方法的参数传递(就像使用其他参数一样)b
。
public static int returnInt(int b) {
return returnInt(b, 0)
}
private static int returnInt(int b, int count) {
// your code here.
}
If (for some strange reason) you cannot change the method signature you can put this variable into ThreadLocal
. In this case even if several threads execute your method simultaneously your code stays thread safe.
如果(出于某种奇怪的原因)您无法更改方法签名,则可以将此变量放入ThreadLocal
. 在这种情况下,即使多个线程同时执行您的方法,您的代码也保持线程安全。
Moving this variable to class level is the worst solution cause it is not thread safe and breaks encapsulation.
将此变量移动到类级别是最糟糕的解决方案,因为它不是线程安全的并且会破坏封装。
回答by PTBG
This function will overflow your stack. You must provide a way out when using recursion. And as for the variable count, you are redefining it back to 0 every time; it cannot be initialized inside the recursive function. Try passing it to your function as a parameter, and also providing a way out for your function.
这个函数会溢出你的堆栈。使用递归时必须提供出路。至于变量计数,您每次都将其重新定义为 0;它不能在递归函数内初始化。尝试将它作为参数传递给您的函数,并为您的函数提供一条出路。
public static int returnInt(int b,int count) {
if (b == 6) {
System.out.println(count + " " + b);
return b;
}
b+=2;
count++;
return returnInt(b);
}
the 'return b' line can be your way out...and you don'y necessarily have to return b... return whatever you need.
'return b' 线可以是你的出路......你不一定要返回 b......返回任何你需要的东西。
回答by DaveJohnston
public static int returnInt(int b) {
return returnInt(b, 0);
}
private static int returnInt(int b, int count) {
if (b == 6) {
System.out.println(count + " " + b);
// My guess is you should be returning something here???
// Actually, this shouldn't be your exit point from the recursion because depending on the starting value of b, since you are always adding 2, it might never equal 6, so again you would get a StackoverflowException.
}
b += 2;
count++;
return returnInt(b, count);
}
回答by Hot Licks
In general, create returnIntInner(int b, boxed count), move the bulk of the logic to that, and call that from a "thin" version of returnInt. To box count
the "old style" way is to pass the count as an int[1] array, so that it can be returned -- I've not studied up on the new Java reference parm syntax. But since your code is tail-recursive and doesn't need to access count
after the recursive call, you can simply pass count
as a regular parameter.
通常,创建 returnIntInner(int b, boxed count),将大部分逻辑移到那个位置,然后从 returnInt 的“瘦”版本调用它。count
将“旧样式”装箱的方法是将计数作为 int[1] 数组传递,以便它可以返回——我还没有研究过新的 Java 引用 parm 语法。但是由于您的代码是尾递归的并且不需要count
在递归调用后访问,您可以简单地count
作为常规参数传递。