Ivor Horton<br />Author of the Beginning Java Series including the new <a href="http://www.amazon.com/exec/obidos/ASIN/1861005695/ref=ase_electricporkchop" target="_blank" rel="nofollow">Beginning Java 2 SDK 1.4 Edition</a>
Originally posted by Ivor Horton:
int[] numbers1 = new int[0]; // compiles
This works but is not very useful. The variable numbers1 references an array of type int[] that has no elements, so you can't store any data in it.
Ivor Horton<br />Author of the Beginning Java Series including the new <a href="http://www.amazon.com/exec/obidos/ASIN/1861005695/ref=ase_electricporkchop" target="_blank" rel="nofollow">Beginning Java 2 SDK 1.4 Edition</a>
Originally posted by Ivor Horton:
You have a valid point Corey. You can certainly pass an array of zero length to a method. It's a good idea to check for a null argument though. Even if you allow for zero-length arrays, you can still have null passed to a method
Originally posted by Ivor Horton:
You must at least define the first dimension as in the previous example.
You cannot specify the second dimension without specifying the first.(You can do the reverse tho')
Ivor Horton<br />Author of the Beginning Java Series including the new <a href="http://www.amazon.com/exec/obidos/ASIN/1861005695/ref=ase_electricporkchop" target="_blank" rel="nofollow">Beginning Java 2 SDK 1.4 Edition</a>
Ivor Horton<br />Author of the Beginning Java Series including the new <a href="http://www.amazon.com/exec/obidos/ASIN/1861005695/ref=ase_electricporkchop" target="_blank" rel="nofollow">Beginning Java 2 SDK 1.4 Edition</a>
Originally posted by Ivor Horton:
Now consider a two-dimensional array:
int[][] numbers = new int[5][6];
If we did not specify the dimension 5, for the first dimension, how many arrays of six integers should there be? And where would their addresses be stored?
Ivor Horton<br />Author of the Beginning Java Series including the new <a href="http://www.amazon.com/exec/obidos/ASIN/1861005695/ref=ase_electricporkchop" target="_blank" rel="nofollow">Beginning Java 2 SDK 1.4 Edition</a>
I'm not sure what you would expect your statement:
int[] numbers = new int[];
to mean. Since there is no dimension specified for the array on the RHS, the array cannot be defined. What space should be allocated with this form and what should be stored in it.
Braces are used to enclose a set of values to be stored in an array and to define the dimensions of the array, so your statement:
int[][] numbers = {null, null};
implies you are defining a two-dimensional array based on a one-dimensional set of two elements with the value null. The values between the braces are inconsistent with the type of element to be stored in the array - type int. The dimensionality of the data values(1-dimensional) is inconsistent with the dimensionality of the array type(2-dimensional).
[qb]
The values between the braces are inconsistent with the type of element to be stored in the array - type int.
[QB]
Do you want ants? Because that's how you get ants. And a tiny ads:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|