wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Question about One-Dimensional and Two-Dimensional Arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about One-Dimensional and Two-Dimensional Arrays" Watch "Question about One-Dimensional and Two-Dimensional Arrays" New topic

Question about One-Dimensional and Two-Dimensional Arrays

Sam Guthrie

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

Joined: Jan 28, 2003
Posts: 4165

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

Joined: Oct 02, 2003
Posts: 11153

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.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 37936
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.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Question about One-Dimensional and Two-Dimensional Arrays
Similar Threads
Reg :- Java Multi Dimension Array
Sierra and Bates Master Exam Question Question
Array Reference Assignments
Master Exam Array question
Multi dimansional Array,Master Exam