在java中初始化一个布尔数组 [英] initializing a boolean array in java

查看:68
本文介绍了在java中初始化一个布尔数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码

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

有人能告诉我我在这里到底做错了什么,我将如何纠正它?我只需要将所有数组元素初始化为 Boolean false.谢谢

could someone tell me what exactly i'm doing wrong here and how would i correct it? I just need to initialize all the array elements to Boolean false. thank you

推荐答案

我只需要将所有数组元素初始化为 Boolean false.

或者使用 boolean[] 来代替所有的值都默认为 false:

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

boolean[] array = new boolean[size];

使用 Arrays#fill()Boolean.FALSE:

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

还要注意数组索引是从零开始的.freq[Global.iParameter[2]] = false; 行会导致 ArrayIndexOutOfBoundsException.要了解有关 Java 数组的更多信息,请参阅 此基本 Oracle 教程.

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.

这篇关于在java中初始化一个布尔数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆