initializing a boolean array in java(在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中初始化一个布尔数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在java中初始化一个布尔数组
基础教程推荐
- 如何对 Java Hashmap 中的值求和 2022-01-01
- 问题http://apache.org/xml/features/xinclude测试日志4j 2 2022-01-01
- Spring AOP错误无法懒惰地为此建议构建thisJoinPoin 2022-09-13
- REST Web 服务返回 415 - 不支持的媒体类型 2022-01-01
- 无法复制:“比较方法违反了它的一般约定!" 2022-01-01
- 使用堆栈算法进行括号/括号匹配 2022-01-01
- RabbitMQ:消息保持“未确认"; 2022-01-01
- 修改 void 函数的输入参数,然后读取 2022-01-01
- Struts2 URL 无法访问 2022-01-01
- 存储 20 位数字的数据类型 2022-01-01
