Initializing a boolean array to false

前端 未结 2 1322
后悔当初
后悔当初 2021-02-02 08:20

I have this piece of code below. How do I initialize each element = false?

boolean[] seats = new boolean[10]

I saw a similar question. But, the

2条回答
  •  故里飘歌
    2021-02-02 08:59

    The default value for the elements in a boolean[] is false. You don't need to do anything.

    The reason it's necessary for Boolean[] is because the default value is null.


    To initialize to true, use the overload of Arrays.fill that accepts a boolean[].

    boolean[] seats = new boolean[10];
    Arrays.fill(seats, true);
    

    See it working online: ideone

提交回复
热议问题