-1

Consider a boolean array with four elements. Initially, all elements are false. The do-while loop will execute some tests and change each element's state until until all four elements are true.

boolean[] pickedSuits = new boolean[4];

do {
    ...some testing...

} while (pickedSuits[0] != true && pickedSuits[1] != true && pickedSuits[2] != true && pickedSuits[3] != true);

Is there a Java way to compare a range of elements instead of testing each one individually? I'm thinking without a for-loop, something like:

while (pickedSuits[0-3] != true);
aaronvan
  • 316
  • 1
  • 3
  • 9
  • 1
    Yes, there is a Java way: Write your own helper method: `boolean isAllTrue(boolean[] arr, int start, int end)`. Usually in a method like that, the `end` index is *exclusive*, meaning that your call would be `isAllTrue(pickedSuits, 0, 4)`. – Andreas Mar 06 '16 at 22:15
  • @rburny Doesn't do the *range* part, but otherwise same thing. Of course, OP *is* checking full array, so maybe it is truly a duplicate. – Andreas Mar 06 '16 at 22:18

2 Answers2

2

Although there is no boolean stream, you can use an IntStream for this.

do {
    ...
} while (IntStream.range(0, pickedSuits.length).noneMatch(i -> pickedSuits[i]));
khelwood
  • 55,782
  • 14
  • 81
  • 108
1

As mentioned in the comments, you can write a helper method:

boolean rangeIsTrue(boolean[] arr, int start, int end) {
    for(int i = start; i < end; i++) if(!arr[i]) return false;
    return true;
}
Marv
  • 3,517
  • 2
  • 22
  • 47