| Author |
Question about One-Dimensional and Two-Dimensional Arrays
|
Sam Guthrie
Greenhorn
Joined: Nov 13, 2012
Posts: 1
|
|
Hello I've been looking at questions from the SCJP Sun JAVA questions these days, and I came across this one that I don't quite understand
C. b2[0][1][1] = b;
D. b2[0][1][1] = b[1][0];
Can someone explain why are these choices wrong?
The solutions say that C is wrong because it tries to assign a two-dimensional array where a one-dimensional array is expected. but I don't see a 1-dimensional array anywhere in the code....
Also, D is wrong because it tries to assign a primitive short where a short array is expected, can someone explain this please? Thanks, I'm not quite getting this right now.
|
 |
Steve Luke
Bartender
Joined: Jan 28, 2003
Posts: 3036
|
|
Can you QuoteYourSources so we know where the question came from?
As written, the reason those two options are wrong is because there is no b2 variable in your code. But then again, your code can't compile because you define the variable named b multiple times.
|
Steve
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9950
|
|
|
one technicality...There is no such thing as a two dimensional array in java. There are only one dimensional arrays, but those can hold arrays, which can hold arrays, which can...and so on.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32704
|
|
|
So you should be able to work out whether those arrays are the right type to fit there. Remember the types are arrayOfSomething, arrayOfArraysOfSomething, arrayOfArraysOfArraysOfSomething, etc.
|
 |
 |
|
|
subject: Question about One-Dimensional and Two-Dimensional Arrays
|
|
|