It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assignment To An Array Refrence Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assignment To An Array Refrence" Watch "Assignment To An Array Refrence" New topic
Author

Assignment To An Array Refrence

sandhi mridul
Ranch Hand

Joined: Jan 25, 2008
Posts: 71
Please please someone help me.
I am very much confuse with this topic.
This is from the first chapter of K&B and is the 12th question.
How the assignment is going on? what is the logic behind?
In the chapter I read that "the array you are assigning must be the same dimension as the refrence you are assigning it to". Then what is going on in this question.
question is:

public class Test{
public static void main(string [] arg){
byte [][]big=new byte[7][7];
byte [][]b=new byte [2][1];
byte b3=5;
byte b2[][][][]=new byte[2][3][1][2];
//
//}}
Which code can be inserted at "//" so the the code compile fine.
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;

Ans:-a,b,e,f

Will really appreciate all your help.
Thanks
Sandhya
Thiago Furuchima
Greenhorn

Joined: Jan 30, 2008
Posts: 11
Hi sandhi, how are you?

I think i know where you get confused... you're misunderstanding "dimension" and "size".
So, K&B is right. "the array you are assigning must be the same dimension as the refrence you are assigning it to"

Using your code as example:
the expression "b2[1][2]" expects "any" two dimension array of byte, no matter the real size of the array.
sandhi mridul
Ranch Hand

Joined: Jan 25, 2008
Posts: 71
Hi Thiago,

Thanks for answering me.I m good and wish the same for you.
As per your explanation as far as I got. Size of the array doesn't matter but the dimension do.
As per this the option a and f is right. What about b and e then?
Because in 'b' option the byte b is just the byte value not array and e option 2d array is being assigned to 4d.
I am not getting this.

sandhya
Paul Brabban
Ranch Hand

Joined: Jan 13, 2008
Posts: 44
Hi,

I think that (b) is correct because it's assigning b (a byte) to a reference that's defined as a byte.

I think (e) is correct because it's assigning an element in the last dimension of a 2-d array to an element in the last dimension of a 4-d array.

If I get this right, in any array only the last dimension contains the type the array is defined as - all the others contain references to array objects, i.e.

byte [2] [3] [2] [1]
byte-arrays byte-arrays byte-arrays bytes

So for any dimension of an array (except the last one) you'll be able to assign a reference to an array with the right number of dimensions to fit. For the last dimension, you're able to assign references to the type the array is typed to,

Hope that helps - I have to say I find this stuff tough to visualize

Cheers

Paul


"The important thing is not to stop questioning."
Paul Brabban
Ranch Hand

Joined: Jan 13, 2008
Posts: 44
Oh - the forum reformatted my post. Lets see if the code tags help

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Assignment To An Array Refrence
 
Similar Threads
Self Test Question on Arrays by Kathy's Book
Arrays
scjp1.4 qtn on Mult-dimensional array
Once more about Array
How This Array Looks