我在 Java 中使用数组时遇到一些问题。如果我像这样声明一个字符数组,我的程序将抛出“out of bound array”异常:
char[] ipx = {};
for( int i =0; i <= 63 ; i++ ){
ipx[i] = myString.charAt(i);
}
我不知道为什么当我将第一行替换为:
char[] ipx = new char[64];
我认为它们都是正确的,因为我曾经像这样删除新字符串:
String newString = "";
它们之间有什么区别? 非常感谢您提供的任何帮助
最佳答案
I do not know why it is ok when i replace the first line by:
因为 char[] ipx = {};
等同于 char[] ipx = new char[0];//大小为零的数组;
在后一种情况下,char[] ipx = new char[64];
您为数组分配了 64 个字符。
关于java - 是否可以在没有元素数量的情况下在 Java 中构造数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19734527/