initializing a boolean array in java

前端 未结 6 1067
花落未央
花落未央 2020-12-23 08:35

I have this code

public static Boolean freq[] = new Boolean[Global.iParameter[2]];
freq[Global.iParameter[2]] = false;

could someone tell m

6条回答
  •  时光取名叫无心
    2020-12-23 09:16

    I just need to initialize all the array elements to Boolean false.

    Either use boolean[] instead so that all values defaults to false:

    boolean[] array = new boolean[size];
    

    Or use Arrays#fill() to fill the entire array with Boolean.FALSE:

    Boolean[] array = new Boolean[size];
    Arrays.fill(array, Boolean.FALSE);
    

    Also note that the array index is zero based. The freq[Global.iParameter[2]] = false; line as you've there would cause ArrayIndexOutOfBoundsException. To learn more about arrays in Java, consult this basic Oracle tutorial.

提交回复
热议问题