Page 1 of 1

About Question enthuware.ocajp.i.v7.2.969 :

Posted: Sun Dec 23, 2012 10:23 pm
by ETS User
I thought the following would be incorrect because the {1, 2, 3} specified three values and the array was two dimensional:

int [] i [] = { {1, 2}, {1}, { }, {1, 2, 3} };

Is the {1, 2, 3} somehow grouped into just two values?

Re: About Question enthuware.ocajp.i.v7.2.969 :

Posted: Mon Dec 24, 2012 7:29 am
by admin
The important thing to remember about multi-dimensional arrays in java is that they are not like matrices. i.e. don't have to be symmetrical (2x3, 2x2, 4x4 etc). They are just arrays of arrays. So when you have a two dimensional array, you can visualize it like an.s array of pointers to other arrays as shown in the image below. So each pointer in the first dimension can point to another array, which in turn, can itself be an array of pointers and can be of any length.

In the given question, the first dimension is an array of just pointers (to arrays of ints). The pointers are all the similar but the arrays to which they point to are of different lengths.
arrays.gif
arrays.gif (2.94 KiB) Viewed 3423 times
HTH,
Paul.

BTW, there is a minor difference in the array in the question and the array in the image show above. The third element in the question is not pointing to null. It is pointing to another array of length 0. But in the image the third element is shown as null.

Re: About Question enthuware.ocajp.i.v7.2.969 :

Posted: Mon Dec 24, 2012 11:41 am
by ETS user
I see now. Thank you for the picture and the explanation. It's much appreciated.

Re: About Question enthuware.ocajp.i.v7.2.969 :

Posted: Mon Apr 06, 2015 6:53 am
by EelcoD
Can someone explain to me why "int [] i []" is correct?
I have never seen a declaration of a multidimensional array this way.
What's that i doing there?

Thnx!

Re: About Question enthuware.ocajp.i.v7.2.969 :

Posted: Mon Apr 06, 2015 10:57 am
by admin
i is the name of the variable. It is a valid way to define a multidimensional array. You need to go through this topic from a book to understand how it works. It is too important to be explained here.