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

后端 未结 11 917
后悔当初
后悔当初 2020-11-27 18:23

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?

11条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 18:40

    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 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);
    }
    

提交回复
热议问题