Can I construct arrays in Java without the number of elements?

There are some problems when using arrays in Java If I declare a character array like this, my program will throw an exception "out of bound array":

char[] ipx = {};
for( int i =0; i <= 63 ; i++ ){
ipx[i] = myString.charAt(i);
}

I don't know why I can change the first line:

char[] ipx = new char[64];

I think they are all right, because I once announced a new string like this:

String newString = "";

What's the difference between those? Thank you very much for any help

Solution

Because char [] IPX = {}; Equivalent to char [] IPX = new char [0]// Zero size array; In the latter case, char [] IPX = new char [64]; You assign 64 characters to your array

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>