Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the most elegant way to check if all values in a boolean array are true?

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?

like image 501
donturner Avatar asked Nov 24 '11 17:11

donturner


People also ask

How do you check if all values in a boolean array are true?

To check if all values in an array are truthy, use the every() method to iterate over the array and return each value straight away. If all values in the array are truthy, the every method will return true , otherwise it returns false .

How do you check if all values in an array are true?

The every() method executes a function for each array element. The every() method returns true if the function returns true for all elements. The every() method returns false if the function returns false for one element.

How do you check if every element in an array is false?

To check if all values in an array are falsy, use the every() method to iterate over the array, convert each value to boolean, negate it, and return the result. If all values in the array are falsy, the every method will return true .


1 Answers

public static boolean areAllTrue(boolean[] array) {     for(boolean b : array) if(!b) return false;     return true; } 
like image 109
Eng.Fouad Avatar answered Sep 19 '22 22:09

Eng.Fouad