• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Array confusion  RSS feed

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
a alph
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ruben for your very clear explanation.

Regards
alph
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!