Java递归问题
下面是我的一个类中的一段代码:
public void run() {
SomeClass[][] someClassArray = new SomeClass[6][];
someClassArray[0] = new SomeClass[1];
someClassArray[1] = new SomeClass[4];
someClassArray[2] = new SomeClass[16];
someClassArray[3] = new SomeClass[64];
someClassArray[4] = new SomeClass[256];
someClassArray[5] = new SomeClass[1024];
someFunction(0,0);
}
someFunction(int i, int j) {
if(i == 5) {
someClassArray[i][j].flag = test(i,j); // BASE CASE
}
else {
if(someFunction(someClassArray(i+1,j*4))
&& someFunction(someClassArray(i+1,j*4+1))
&& someFunction(someClassArray(i+1,j*4+2))
&& someFunction(someClassArray(i+1,j*4+3)))
someClassArray[i][j].flag = true;
else
someClassArray[i][j].flag = false;
}
return someClassArray[i][j].flag;
}
class SomeClass {
boolean flag;
// other stuff
}
boolean test(int i, int j) {
// test some property of this coordinate
}
本质上,我想要的是仅当下一个数组级别中的四个相应对象的标志也为 true 时,才将一个 SomeClass 对象的标志设置为 true。不幸的是,我似乎遇到了问题:
if(someFunction(someClassArray(i+1,j*4))
&& someFunction(someClassArray(i+1,j*4+1))
&& someFunction(someClassArray(i+1,j*4+2))
&& someFunction(someClassArray(i+1,j*4+3)))
看起来它只是检查第一个条件(我在计数器中添加了因此检查 someFunction 被调用的次数,它最多只达到 6,而不是我应该得到的 1365 );你不能在Java中的if语句中执行多个函数调用吗?或者我做错了?
(顺便说一句,抱歉格式问题;这是我第一次在这里发帖)
Here's a section of code from one of my classes:
public void run() {
SomeClass[][] someClassArray = new SomeClass[6][];
someClassArray[0] = new SomeClass[1];
someClassArray[1] = new SomeClass[4];
someClassArray[2] = new SomeClass[16];
someClassArray[3] = new SomeClass[64];
someClassArray[4] = new SomeClass[256];
someClassArray[5] = new SomeClass[1024];
someFunction(0,0);
}
someFunction(int i, int j) {
if(i == 5) {
someClassArray[i][j].flag = test(i,j); // BASE CASE
}
else {
if(someFunction(someClassArray(i+1,j*4))
&& someFunction(someClassArray(i+1,j*4+1))
&& someFunction(someClassArray(i+1,j*4+2))
&& someFunction(someClassArray(i+1,j*4+3)))
someClassArray[i][j].flag = true;
else
someClassArray[i][j].flag = false;
}
return someClassArray[i][j].flag;
}
class SomeClass {
boolean flag;
// other stuff
}
boolean test(int i, int j) {
// test some property of this coordinate
}
Essentially what I want is to set the flag of one SomeClass object to true only if the four corresponding objects' flags in the next array level are also true. Unfortunately, I seem to be having problems with this:
if(someFunction(someClassArray(i+1,j*4))
&& someFunction(someClassArray(i+1,j*4+1))
&& someFunction(someClassArray(i+1,j*4+2))
&& someFunction(someClassArray(i+1,j*4+3)))
It looks like it's only checking the first condition (I added in a counter so check how many times someFunction is called and it only went up to 6, rather than the 1365 I should be getting); are you not able to do multiple function calls in an if statement in Java? or am I doing it wrong?
(Sorry about the formatting by the way; this is my first time posting here)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
操作数
&&
和||
称为 短路运算符。这意味着如果结果已知,它们所在的逻辑将立即停止执行。在您的情况下,这可能意味着第一次调用
SomeFunction
的结果是false
。因为该行上只有 AND,所以结果不可能再为true
,因此您对SomeFunction
的其他 3 次调用不会被执行。底线:将
&&
更改为&
,这不会短路并且所有调用都会被执行。The operands
&&
and||
are so called short-circuiting operators. This means the logic they are in will immediately stop executing if the result in known.In your case, it probably means that the result of the first call to
SomeFunction
isfalse
. Because you only have AND on that line, the result can't possibly betrue
anymore and thus your other 3 calls toSomeFunction
don't get executed.Bottom-line: change the
&&
to&
, this won't short-circuit and all call will get executed.如果条件的结果在第一次函数调用时就已明确,则可以将条件短路。
If condition can be short-circuited if its outcome is clear from the first function call.