• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question about One-Dimensional and Two-Dimensional Arrays

 
Sam Guthrie
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12083
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 48374
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic