File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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
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: 4181
    
  21

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: 11402
    
  16

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
Sheriff

Joined: Oct 13, 2005
Posts: 39393
    
  28
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about One-Dimensional and Two-Dimensional Arrays