Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Kathy Book Question - Array References(Urgent)

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Q12 Chap 1 Pg49
public class test{
public static void main(String []args){
byte [][] big = new byte[7][7];
byte [][]b=new byte[2][1];
byte b3=5;
byte b2[][][][]=new byte [2][3][1][2];
//line of code to be inserted.Answer is A,B,E,F
}
}
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 have go through the answer 's explaination,
but can't understand how to get the solution.
Apart from that,I have also refer to diagram in Fig 1-7 Pg31,but still confused.
Can someone please enlighten me,many thanks.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A little trick I use:
Consider the array byte[][][][] b2; and look at A) b2[0][1]. We have only used two indices [][] from the four [][][][] that have been declared, that leaves two [][] left over. So to b2[0][1] we can assign a byte[][]. Now b is a byte[][], it's declared as one, so b2[0][1] = b; is valid.
Look now at C) b2[1][1][0]. We have used three indices [][][] from the four [][][][] that have been declared, and that leaves one [] left over. So to b2[1][1][0] we can assign a byte[]. But b[0][0] is a primitive byte not an array byte[], so we cannot assign it to b2[1][1][0]
Similar arguments for the rest.
[ March 25, 2003: Message edited by: Barry Gaunt ]
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a remark about this same question. I am confused that answer F is correct.
B2 is assigned an created array of the dimensions [2][3][1][2]. I would think that in the second dimension you can put only references to arrays of dimensions [1][2]. I would think that corresponds with the amount of memory that is allocated on the heap. How can answer F be good, if one puts in the element b2[0][1] a reference to a [7][7] array?
Thanks for the answer in advance!
Cor
Update: tested the code
I tested the code and it was possible to do it.

I added a testByte2 to see if the overall structure of the array is changed, but that is not the case. So apparently the original dimensions do not impose limits on the way the elements of the array are filled in with existing, and therefore already memory-allocated, arrays. The only limit is the number of dimensions. If you increase for example the number of dimensions of big from 2 to 3, you get a compiler error "cannot convert from byte[][][] to byte [][]".
[ January 20, 2004: Message edited by: Cor Lieftink ]
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Berry,

Your explanation was very good. THis is the only area i was not comfortable. After going thru u'r explanation , it was crystal clear.
Thanks,
Sanjana
 
What's gotten into you? Could it be this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic