将布尔数组初始化为false [英] Initializing a boolean array to false
本文介绍了将布尔数组初始化为false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我下面有这段代码.如何初始化每个元素= false?
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 second line didnt make sense to me (Can you explain the 2nd line?).
Boolean[] array = new Boolean[size];
Arrays.fill(array, Boolean.FALSE);
推荐答案
boolean[]
中元素的默认值为false.您无需执行任何操作.
The default value for the elements in a boolean[]
is false. You don't need to do anything.
Boolean[]
之所以必要是因为默认值是null
.
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);
查看它是否可以在线运行: ideone
See it working online: ideone
这篇关于将布尔数组初始化为false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文