切换布尔值,但只切换一次? (java)

发布于 2024-12-02 18:11:25 字数 648 浏览 1 评论 0原文

有时需要在循环中只存储一次布尔值(以记录它从 false 变为 true,反之亦然),同时将循环执行到最后,但不再关心布尔值的变化价值。示例:


    public static boolean dbDelete(Collection argObjectCollectionToDelete) {
        boolean result = true;
        for (Object object : argObjectCollectionToDelete) {
            boolean dbDelete = dbDelete(object);
            if (!dbDelete) {
                result = false;
            }
        }
        return result;
    }

是否有某种方法可以执行等效的代码


if (!dbDelete) {
    result = false;
}


if (!dbDelete && !result) {
    result = false;
}

以更优雅的方式(最好是在一行中)?

Every now and then there is a need to store a boolean value only once (to record that it has changed from false to true or vice versa) in a loop, while executing the loop to the end but not caring anymore about changes in the boolean value. Example:


    public static boolean dbDelete(Collection argObjectCollectionToDelete) {
        boolean result = true;
        for (Object object : argObjectCollectionToDelete) {
            boolean dbDelete = dbDelete(object);
            if (!dbDelete) {
                result = false;
            }
        }
        return result;
    }

Is there some way to execute the equivalent of the code


if (!dbDelete) {
    result = false;
}

or


if (!dbDelete && !result) {
    result = false;
}

in a more elegant way, preferrably in one line?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

心如狂蝶 2024-12-09 18:11:25

怎么样:

result &= dbDelete(object);

这相当于:

result = result & dbDelete(object);

所以只有当 result 之前为 true 并且 dbDelete 返回 true 时,它​​才为 true。

How about:

result &= dbDelete(object);

This is equivalent to:

result = result & dbDelete(object);

So it will only be true if result was previously true and dbDelete returned true.

贱贱哒 2024-12-09 18:11:25
if (!dbDelete(object)) {
    result = false;
}
if (!dbDelete(object)) {
    result = false;
}
暗地喜欢 2024-12-09 18:11:25

结果 = dbDelete(对象) ?结果:假;

result = dbDelete(object) ? result : false;

苍景流年 2024-12-09 18:11:25

尝试为变量名称赋予更多含义 - 这会让事情变得更容易。

public static boolean dbDelete(Collection argObjectCollectionToDelete) {
    boolean completedSuccessfully = true;
    for (Object object : argObjectCollectionToDelete) {
        boolean dbDelete = dbDelete(object);
        if (completedSuccessfully && !dbDelete) {  //won't change anymore after failure
            completedSuccessfully = false;
        }
    }
    return completedSuccessfully;
}

Try putting more meaning into your variable names - it makes things easier.

public static boolean dbDelete(Collection argObjectCollectionToDelete) {
    boolean completedSuccessfully = true;
    for (Object object : argObjectCollectionToDelete) {
        boolean dbDelete = dbDelete(object);
        if (completedSuccessfully && !dbDelete) {  //won't change anymore after failure
            completedSuccessfully = false;
        }
    }
    return completedSuccessfully;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文