Java递归问题

发布于 2024-10-31 00:34:58 字数 1613 浏览 1 评论 0原文

下面是我的一个类中的一段代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

箹锭⒈辈孓 2024-11-07 00:34:58

操作数 &&|| 称为 短路运算符。这意味着如果结果已知,它们所在的逻辑将立即停止执行。

在您的情况下,这可能意味着第一次调用 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 is false. Because you only have AND on that line, the result can't possibly be true anymore and thus your other 3 calls to SomeFunction don't get executed.

Bottom-line: change the && to &, this won't short-circuit and all call will get executed.

命比纸薄 2024-11-07 00:34:58

如果条件的结果在第一次函数调用时就已明确,则可以将条件短路。

If condition can be short-circuited if its outcome is clear from the first function call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文