Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Assignment To An Array Refrence

 
sandhi mridul
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Paul Brabban
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh - the forum reformatted my post. Lets see if the code tags help

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic