将布尔数组初始化为false

编程入门 行业动态 更新时间:2024-10-19 17:27:48
本文介绍了将布尔数组初始化为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.

要初始化为true,请使用Arrays.fill的20boolean%29>过载接受boolean[].

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

本文发布于:2023-10-09 05:58:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1474900.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:布尔   数组   false

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!