将布尔数组中的所有值设置为true

Fra*_*ice 6 java arrays variables boolean

Java中是否有一个方法可以将布尔数组中的所有值设置为true?

显然我可以使用for循环来做这个,但是如果我(例如)有一个大的3D数组,我想使用循环会非常低效.

Java中是否有任何方法可以将某个数组中的所有值设置为true,或者在初始化数组时将所有值设置为true?

(例如

boolean[][][] newBool = new boolean[100][100][100];
newBool.setAllTrue();

//Rather than

for(int a = 0; a < 100; a++) {
    for(int b = 0; b < 100; b++) {
        for(int c = 0; c < 100; c++) {
            newBool[a][b][c] = true;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Jac*_*ack 8

您可以使用Java 7的Arrays.fill,它为指定数组的每个元素指定一个指定的值...所以类似于.这仍然使用循环,但写入至少要短.

boolean[] toFill = new boolean[100] {};
Arrays.fill(toFill, true);
Run Code Online (Sandbox Code Playgroud)