检查布尔数组中的所有值是否都为真的最优雅的方法是什么?

发布于 2024-12-17 21:24:48 字数 115 浏览 0 评论 0原文

我在java中有一个布尔数组:

boolean[] myArray = new boolean[10];

检查所有值是否都为真的最优雅的方法是什么?

I have a boolean array in java:

boolean[] myArray = new boolean[10];

What's the most elegant way to check if all the values are true?

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

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

发布评论

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

评论(12

如若梦似彩虹 2024-12-24 21:24:48
public static boolean areAllTrue(boolean[] array)
{
    for(boolean b : array) if(!b) return false;
    return true;
}
public static boolean areAllTrue(boolean[] array)
{
    for(boolean b : array) if(!b) return false;
    return true;
}
悲欢浪云 2024-12-24 21:24:48
Arrays.asList(myArray).contains(false)
Arrays.asList(myArray).contains(false)
旧伤还要旧人安 2024-12-24 21:24:48

在 Java 8 中,你可以这样做:

boolean isAllTrue = Arrays.asList(myArray).stream().allMatch(val -> val == true);

或者更短:

boolean isAllTrue = Arrays.stream(myArray).allMatch(Boolean::valueOf);

注意:
您需要 Boolean[] 才能使此解决方案发挥作用。因为你不能有一个基元列表。

In Java 8, you could do:

boolean isAllTrue = Arrays.asList(myArray).stream().allMatch(val -> val == true);

Or even shorter:

boolean isAllTrue = Arrays.stream(myArray).allMatch(Boolean::valueOf);

Note:
You need Boolean[] for this solution to work. Because you can't have a primitives List.

云醉月微眠 2024-12-24 21:24:48

这取决于您想要查找此信息的次数,如果不止一次:

Set<Boolean> flags = new HashSet<Boolean>(myArray);
flags.contains(false);

否则会出现短路循环:

for (i = 0; i < myArray.length; i++) {
  if (!myArray[i]) return false;
}
return true;

It depends how many times you're going to want to find this information, if more than once:

Set<Boolean> flags = new HashSet<Boolean>(myArray);
flags.contains(false);

Otherwise a short circuited loop:

for (i = 0; i < myArray.length; i++) {
  if (!myArray[i]) return false;
}
return true;
如梦 2024-12-24 21:24:48

我不敢相信没有 BitSet< /code>解决方案。

BitSet 是一组位的抽象,因此我们不必再使用 boolean[] 进行更高级的交互,因为它已经包含了大多数所需的方法。它在批处理操作中也非常快,因为它内部使用 long 值来存储位,因此不会像我们使用 boolean[] 那样单独检查每个位。

BitSet myBitSet = new BitSet(10);
// fills the bitset with ten true values
myBitSet.set(0, 10);

对于您的特定情况,我会使用 cardinality()

if (myBitSet.cardinality() == myBitSet.size()) {
    // do something, there are no false bits in the bitset
}

另一种选择是使用 番石榴

return Booleans.contains(myArray, true);

I can't believe there's no BitSet solution.

A BitSet is an abstraction over a set of bits so we don't have to use boolean[] for more advanced interactions anymore, because it already contains most of the needed methods. It's also pretty fast in batch operations since it internally uses long values to store the bits and doesn't therefore check every bit separately like we do with boolean[].

BitSet myBitSet = new BitSet(10);
// fills the bitset with ten true values
myBitSet.set(0, 10);

For your particular case, I'd use cardinality():

if (myBitSet.cardinality() == myBitSet.size()) {
    // do something, there are no false bits in the bitset
}

Another alternative is using Guava:

return Booleans.contains(myArray, true);
策马西风 2024-12-24 21:24:48

该行应该足够了:

BooleanUtils.and(boolean... array)

但为了让仅链接的纯粹主义者平静下来:

对一组布尔值执行“与”操作。

That line should be sufficient:

BooleanUtils.and(boolean... array)

but to calm the link-only purists:

Performs an and on a set of booleans.

魔法唧唧 2024-12-24 21:24:48

在 Java 8+ 中,您可以在 0myArray.length 范围内创建一个 IntStream 并检查所有值是否为 true 在相应的(原始)数组中,类似于:

return IntStream.range(0, myArray.length).allMatch(i -> myArray[i]);

In Java 8+, you can create an IntStream in the range of 0 to myArray.length and check that all values are true in the corresponding (primitive) array with something like,

return IntStream.range(0, myArray.length).allMatch(i -> myArray[i]);
顾冷 2024-12-24 21:24:48

这可能不会更快,而且绝对不可读。所以,为了丰富多彩的解决方案......

int i = array.length()-1;
for(; i > -1 && array[i]; i--);
return i==-1

This is probably not faster, and definitely not very readable. So, for the sake of colorful solutions...

int i = array.length()-1;
for(; i > -1 && array[i]; i--);
return i==-1
ペ泪落弦音 2024-12-24 21:24:48
boolean alltrue = true;
for(int i = 0; alltrue && i<booleanArray.length(); i++)
   alltrue &= booleanArray[i];

我认为这看起来不错并且表现得很好......

boolean alltrue = true;
for(int i = 0; alltrue && i<booleanArray.length(); i++)
   alltrue &= booleanArray[i];

I think this looks ok and behaves well...

烙印 2024-12-24 21:24:48

您可以通过 Arrays.equal 方法将您的数组与其他布尔数组进行比较,检查所有值项是 true 还是 false,如下例所示:

private boolean isCheckedAnswer(List<Answer> array) {
    boolean[] isSelectedChecks = new boolean[array.size()];
    for (int i = 0; i < array.size(); i++) {
        isSelectedChecks[i] = array.get(i).isChecked();
    }

    boolean[] isAllFalse = new boolean[array.size()];
    for (int i = 0; i < array.size(); i++) {
        isAllFalse[i] = false;
    }

    return !Arrays.equals(isSelectedChecks, isAllFalse);
}

You can check all value items are true or false by compare your array with the other boolean array via Arrays.equal method like below example :

private boolean isCheckedAnswer(List<Answer> array) {
    boolean[] isSelectedChecks = new boolean[array.size()];
    for (int i = 0; i < array.size(); i++) {
        isSelectedChecks[i] = array.get(i).isChecked();
    }

    boolean[] isAllFalse = new boolean[array.size()];
    for (int i = 0; i < array.size(); i++) {
        isAllFalse[i] = false;
    }

    return !Arrays.equals(isSelectedChecks, isAllFalse);
}
胡大本事 2024-12-24 21:24:48

科特林:
如果一个元素为 false,则不会选择所有元素

return list.filter { isGranted -> isGranted.not() }.isNotEmpty()

Kotlin:
if one elemnt is false then not all are selected

return list.filter { isGranted -> isGranted.not() }.isNotEmpty()
静谧幽蓝 2024-12-24 21:24:48

好的。这是我能即时想到的“最优雅”的解决方案:

boolean allTrue = !Arrays.toString(myArray).contains("f");

希望有帮助!

OK. This is the "most elegant" solution I could come up with on the fly:

boolean allTrue = !Arrays.toString(myArray).contains("f");

Hope that helps!

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