K
K
Kirill2018-07-06 00:33:04
Java
Kirill, 2018-07-06 00:33:04

How to make a construction that checks that if all variables in the boolean array are equal to false then do something....?

How to make a construction that checks that if all variables in a boolean array are equal to false, then do this for example:

Toast toast = Toast.makeText(getApplicationContext(), answer , Toast.LENGTH_SHORT);
toast.setGravity(Gravity.TOP, 0, 15);
toast.show();

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexey P, 2018-07-06
@PainMain

Feel free to stream.

Boolean[] array = new Boolean[] {false, false, false};
boolean allFalse = Arrays.stream(array).allMatch((e) -> !e);
// или
allFalse = Arrays.stream(array).noneMatch((e) -> e);

M
Mikhail Ketov, 2018-07-06
@Dugayoyo

If there are few of them, and speed is not important, you can do this:

public static void main(String[] args) {
        Boolean[] array = new Boolean[3];
        array[0] = true;
        array[1] = true;
        array[2] = true;
        boolean allTrue = !Arrays.asList(array).contains(Boolean.FALSE);
        System.out.println(allTrue);
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question