Boolean array can only be used to store Boolean data type values. The default value of a Boolean array is false. Boolean array is initialized to false, reference type array is initialized to null. In some cases we need to initialize all values of boolean array to true or false. In this case, we can use the Arrays.fill() method.
boolean[] booleanArray;
import java.util.Arrays; public class BooleanArrayTest { public static void main(String[] args) { Boolean[] boolArray = new Boolean[5]; // initialize a boolean array for(int i = 0; i < boolArray.length; i++) { System.out.println(boolArray[i]); } Arrays.fill(boolArray, Boolean.FALSE); <strong> </strong> // all the values will be false<strong> </strong> for(int i = 0; i < boolArray.length; i++) { System.out.println(boolArray[i]); } Arrays.fill(boolArray, Boolean.TRUE); <strong> </strong>// all the values will be true<strong> </strong> for (int i = 0; i < boolArray.length; i++) { System.out.println(boolArray[i]); } } }
null null null null null false false false false false true true true true true
The above is the detailed content of In Java, how do we initialize a boolean array?. For more information, please follow other related articles on the PHP Chinese website!