gpt4 book ai didi

java - 是否可以在没有元素数量的情况下在 Java 中构造数组?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:27:39 25 4
gpt4 key购买 nike

我在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com