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

Language Fundamental Question

Ranch Hand
Posts: 71
• Number of slices to send:
Optional 'thank-you' note:
I'm stuck with the following exam question:

which of the following lines of code could be inserted, and still allow the code to compile?

It also says that b2[1][2][0] = b; is incorrect

According to what I understand, b2 is represented like this:

So b2[1][2][0] should hold a reference to an array of length = 2, but because b[][] references an array with an extra dimension, it cannot work. (Is that correct?)

...but

The answers given to be correct are:
b2[0][1] = b;

How can this be correct? I would have though that b2[0][1] would have been expecting a reference to a 2D array like the following

Or does it not matter what the length of the array is, only that is should be two dimensional?

Thoughts appreciated.
[ March 15, 2006: Message edited by: Arthur Blair ]

Ranch Hand
Posts: 2412
• Number of slices to send:
Optional 'thank-you' note:
That is correct. The length of the array is not important. You have to make sure the dimension matches.

Bartender
Posts: 6663
5
• Number of slices to send:
Optional 'thank-you' note:
An easy way to solve the problem is...

Check the statement b2[1][2][0] = b ;

If the number of square brace pairs left with b2 (b2 has one brace pair left )

equals the number of brace pairs that the assigned array has ( b has 2 brace

pairs ) then the assignment is legal. So if b had just one dimension the

assignment would have compiled.

 With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.