本文介绍了在Java中初始化布尔数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个代码
public static Boolean freq[] = new Boolean[Global.iParameter[2]];
freq[Global.iParameter[2]] = false;
有人可以告诉我我到底在做什么错,我该如何纠正?我只需要将所有数组元素初始化为布尔型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
:
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中初始化布尔数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!