Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to contruct array in Java without number of elements?

I have some problem when using array in Java. If I declare an array of character like this, my program will throw exception "out of bound array":

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

I do not know why it is ok when i replace the first line by:

char[] ipx = new char[64];

I think both of them are correct because i used to delare new string like this:

String newString = "";

what's the difference between those ? Many thanks for any help you may be able to provide

like image 572
Huyết Công Tử Avatar asked Apr 09 '26 07:04

Huyết Công Tử


1 Answers

I do not know why it is ok when i replace the first line by:

Because char[] ipx = {}; is equivalent to char[] ipx = new char[0]; // zero sized array;
and in latter case char[] ipx = new char[64]; you allocate 64 chars for your array.

like image 120
kiruwka Avatar answered Apr 11 '26 21:04

kiruwka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!