GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Array confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Array confusion" Watch "Array confusion" New topic
Author

Array confusion

a alph
Ranch Hand

Joined: Nov 18, 2008
Posts: 36
Hi,

This is from K&B book.



which of the following codes could be inserted at line 7, and still allow the code to compile? (choose four).

a) b2 [0][1] = b;
b) b [0][0] = b3;
c) b2 [1][1][0] = b [0][0];
d) b2 [1][2][0] = b;
e) b2 [0][1][0][0] = b [0][0];
f) b2 [0][1] = big;

I couldn't understand how to work it out the dimensions.
Can anyone please help.

Thanks.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
You just need to make sure that the reference types are the same in the LHS and RHS:


And to elaborate:

byte[][][][] b;
Now, b is a 4-dimensional byte array.
This means:
b[0] will be a 3-dimensional byte array, or a reference of type byte[][][]
b[0][0] will be a 2-dimensional byte array, or a reference of type byte[][]
b[0][0][0] will be a 1-dimensional byte array, or a reference of type byte[]
b[0][0][0][0] will be a reference of type byte (you can think of it as a 0-dimensional byte array)


All code in my posts, unless a source is explicitly mentioned, is my own.
a alph
Ranch Hand

Joined: Nov 18, 2008
Posts: 36
Thanks Ruben for your very clear explanation.

Regards
alph
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
No problem!
 
GeeCON Prague 2014
 
subject: Array confusion