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?
About Question enthuware.ocajp.i.v7.2.969 :
Moderator: admin
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.969 :
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.
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.
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.
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 :
I see now. Thank you for the picture and the explanation. It's much appreciated.
-
- Posts: 10
- Joined: Sat Jan 24, 2015 8:09 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.969 :
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!
I have never seen a declaration of a multidimensional array this way.
What's that i doing there?
Thnx!
-
- Site Admin
- Posts: 10388
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.969 :
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.
Who is online
Users browsing this forum: No registered users and 3 guests