使用递归如何保持局部变量更新
使用递归如何保持局部变量更新直到满足条件。我下面有一个例子很好地解释了为什么要问这个问题。计数变量是一个局部变量,每次方法执行时计数都会设置为 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);
}
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)
public static int returnInt(int b) {
int count = 0;
if (b == 6) {
System.out.println(count + " " + b);
}
b += 2;
count++;
return returnInt(b);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
将
count
作为附加参数传递给该方法:然后,使用额外参数
0
调用returnInt
以开始操作。Pass
count
as an additional parameter to the method:Then, call
returnInt
with an extra argument of0
to start things off.你不能。局部变量根据定义是局部的。它仅存在于该方法的当前执行范围内。
但你有以下解决方案。
最好的方法是将其作为方法的参数传递(与其他参数
b
完全相同)。如果(由于某种奇怪的原因)您无法更改方法签名,您可以将此变量放入 <在这种情况下,即使多个线程同时执行您的方法,您的代码仍保持线程安全,
这是最糟糕的解决方案,因为它不是线程安全的并且破坏了封装。
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
.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.Moving this variable to class level is the worst solution cause it is not thread safe and breaks encapsulation.
这个函数会溢出你的堆栈。使用递归时必须提供出路。至于变量count,你每次都将它重新定义回0;它不能在递归函数内初始化。尝试将它作为参数传递给您的函数,并为您的函数提供一条出路。
“return b”行可以是您的出路...并且您不一定必须 return b...返回您需要的任何内容。
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.
the 'return b' line can be your way out...and you don'y necessarily have to return b... return whatever you need.
一般来说,创建 returnIntInner(int b, 装箱计数),将大部分逻辑移至该位置,然后从 returnInt 的“精简”版本调用它。装箱
count
的“旧式”方式是将计数作为 int[1] 数组传递,以便可以返回它——我还没有研究新的 Java 参考 parm 语法。但由于您的代码是尾递归的,并且不需要在递归调用后访问count
,因此您只需将count
作为常规参数传递即可。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 accesscount
after the recursive call, you can simply passcount
as a regular parameter.