File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Master Exam Array question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Master Exam Array question" Watch "Master Exam Array question" New topic

Master Exam Array question

AustinRay Lee

Joined: Dec 30, 2003
Posts: 3
Can someone explain the answer to this problem, master exam says that answer C below "is wrong because C tries to assign a two-dimensional array where a one-dimensional array is expected." I can see that b is a two-dimensional array, but it looks like b2 is four-dimensional array. So I would expect the answer to say "C is wrong because C tries to assign a two-dimensional array where a four-dimensional array is expected." Or why is b2 considered a one-dimensional array when it has 4 []'s.
public class Test{
public static void main(String [] args){
short [][] b = new short [4] [4];
short [][]big = new short [2][2];
short b3 = 8;
short b2 [][][][] = new short [2][3][2][2];
// insert code here
A b2[1][1] = big;
B b[1][0] = b3;
C b2[0][1][1] = b:
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Let's start simple and build up ...

Whew. Looks like what you can put into any slot is enough dimensions to fill out the original declaration. In "a[1]" that means no more dimensions, only an int. In d[1][1][1] as in your question, only one more dimension.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
I agree. Here's the link:
subject: Master Exam Array question