在java中初始化一个布尔数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2364856/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
initializing a boolean array in java
提问by leba-lev
I have this code
我有这个代码
public static Boolean freq[] = new Boolean[Global.iParameter[2]];
freq[Global.iParameter[2]] = 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。谢谢你
采纳答案by BalusC
I just need to initialize all the array elements to Boolean false.
我只需要将所有数组元素初始化为 Boolean false。
Either use boolean[]
instead so that all values defaults to false
:
要么使用,boolean[]
以便所有值默认为false
:
boolean[] array = new boolean[size];
Oruse Arrays#fill()
to fill the entire array with Boolean.FALSE
:
或用于Arrays#fill()
填充整个数组Boolean.FALSE
:
Boolean[] array = new Boolean[size];
Arrays.fill(array, Boolean.FALSE);
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.
另请注意,数组索引是从零开始的。该freq[Global.iParameter[2]] = false;
行如你有会导致ArrayIndexOutOfBoundsException
。要了解有关 Java 中数组的更多信息,请参阅此基本 Oracle 教程。
回答by J?rgen Fogh
The array will be initialized to false when you allocate it.
分配数组时,该数组将被初始化为 false。
All arrays in Java are initialized to the default value for the type. This means that arrays of ints are initialised to 0, arrays of booleans are initialised to false and arrays of reference types are initialised to null.
Java 中的所有数组都初始化为该类型的默认值。这意味着整数数组初始化为 0,布尔数组初始化为 false,引用类型数组初始化为 null。
回答by oneat
public static Boolean freq[] = new Boolean[Global.iParameter[2]];
public static Boolean freq[] = new Boolean[Global.iParameter[2]];
Global.iParameter[2]:
Global.iParameter[2]:
It should be const value
它应该是常量值
回答by codaddict
They will be initialized to false
by default. In Java arrays are created on heap and every element of the array is given a default value depending on its type. For boolean
data type the default value is false
.
false
默认情况下,它们将被初始化。在 Java 中,数组是在堆上创建的,数组的每个元素都根据其类型被赋予一个默认值。对于boolean
数据类型,默认值为false
。
回答by Big Endian
Arrays in Java start indexing at 0. So in your example you are referring to an element that is outside the array by one.
Java 中的数组从 0 开始索引。因此,在您的示例中,您指的是数组外部的一个元素。
It should probably be something like freq[Global.iParameter[2]-1]=false;
它可能应该类似于 freq[Global.iParameter[2]-1]=false;
You would need to loop through the array to initialize all of it, this line only initializes the last element.
您需要遍历数组以初始化所有数组,此行仅初始化最后一个元素。
Actually, I'm pretty sure that false is default for booleans in Java, so you might not need to initialize at all.
实际上,我很确定 false 是 Java 中布尔值的默认值,因此您可能根本不需要初始化。
Best Regards
此致