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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

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

Post 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?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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 3419 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.

ETS user

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

Post by ETS user »

I see now. Thank you for the picture and the explanation. It's much appreciated.

EelcoD
Posts: 10
Joined: Sat Jan 24, 2015 8:09 am
Contact:

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

Post 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!

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests