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?
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 .
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.
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 .
public static boolean areAllTrue(boolean[] array) { for(boolean b : array) if(!b) return false; return true; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With