Win a copy of Micro Frontends in Action this week in the Server-Side JavaScript and NodeJS forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Ron McLeod
• Paul Clapham
• Bear Bibeault
• Junilu Lacar
Sheriffs:
• Jeanne Boyarsky
• Tim Cooke
• Henry Wong
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• salvin francis
• Frits Walraven
Bartenders:
• Scott Selikoff
• Piet Souris
• Carey Brown

# Multidimensional array Qn

Ranch Hand
Posts: 76
Hi,

I did the exam in Chapter 1 of Kathy Sierra's Java 2 Sun Certified programmer and developer book for Java 1.4. One question is confusing me a litte and hopefully somebody can help??

Question 12:
There are 2 multidimensional arrays:
byte [][] big = new byte [7][7];
byte b2 [][][][] = new byte [2][3][1][2];

We are then asked to choose which lines of code would allow the program to compile. One of the lines according to the book is:
b2[0][1] = big;

From what I understand it is possible to assign a 2D array to b2[0][1], however the dimensions of the big array are [7][7] - so I don't understand why the above answer is correct when the last 2 dimensions of the b2 array are [1][2] ...

Can anybody please shed some light on this for me.

Many Thanks

Ranch Hand
Posts: 81
From what I understand,
considering byte b2 [][][][] = new byte [2][3][1][2];
b2[0][1] holds a reference to a double dimensional array of size [1][2]. So, by assigning "big" to b2[0][1], you are just changing the reference, but not really assigning a fixed value.

d jones
Ranch Hand
Posts: 76
Thanks Vijay,

Does b2[0][1] not hold a reference to an array with specific dimensions [1][2] ??

Has it not assigned memory to an array of specific dimensions? How can it then hold be assigned to 2-D array with greater dimensions than for what it was constructed??

By reassigning it is it not like redimensioning array?

Sorry am still confused!!

Ranch Hand
Posts: 81
b2[0][1] definitely holds a reference to an array with the specific dimensions [1][2]. But again, since it is a reference, the TYPE of reference has to be considered. Here in this case, the type of the reference is "TWO DIMENSIONAL" "BYTE" "OBJECT". Now, big, too is "TWO DIMENSIONAL" "BYTE" "OBJECT", of size 7,7. So, big's reference, which points to an object of type byte with 7,7 as it's size, CAN BE assigned to b2[0][1].
As an analogy,
If you have two String objects, String s1 = "hello", and String s2 = "world", you point s1 to s2 by saying s1 = s2, and print s1, you would get "world", right?

So yes, By reassigning it IS like reassigning the size of the array. I mean, I'm sure if I'm wrong, somebody would correct me, but I'm almost certain that my concept is correct. Try printing out the length of the array b2[0][1] after reassigning it to big. That should solve a lot of confusion I guess.

Thanks,
-Vijay

Greenhorn
Posts: 1
I just ran a program to test the dimension of b2[0][1] and it seems that its dimension is 7*7. the program is:
public class scjp {
public static void main(String[] args)
{
byte [][] big = new byte [7][7];
byte b2 [][][][] = new byte [2][3][1][2];
b2[0][1] = big;
System.out.println("the size of b2[0][1] is:"+b2[0][1].length);
System.out.println("the size of b2[0][1][0] is:"+b2[0][1][0].length);
System.out.println("the size of b2[0][1][6] is:"+b2[0][1][6].length);
}
}

and the output is:
the size of b2[0][1] is:7
the size of b2[0][1][0] is:7
the size of b2[0][1][6] is:7

so, from these results, i think b2[0][1] has been changed to dimension 7 by 7. correct me if I'm wrong. Thanks

author
Posts: 23883
142
Basically, Java doesn't have multidimenional arrays -- in the traditional sense. It has arrays of arrays (of arrays, etc). So the declaration is basically creating a bunch of arrays of arrays which points to arrays, etc., instead of just allocation one block.

When you did the assignment, it just redirected the array of array reference to another array of array. And the previous array of array should now be eligible for garbage collection.

Henry

d jones
Ranch Hand
Posts: 76
Thanks for your help with this.

Greenhorn
Posts: 1
Hii
I am just planning to give the SCJP 1.4 exam..
However, I am extremely confused about the 4D and 3D arrays...
can anyone explain to me in depth how to visualise these arrays or if anyone knows any such tutorial..pls do let me know..
Thank u all so much!!

 I brought this back from the farm where they grow the tiny ads: the value of filler advertising in 2020 https://coderanch.com/t/730886/filler-advertising