The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on multi dimensional array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on multi dimensional array" Watch "Question on multi dimensional array" New topic
Author

Question on multi dimensional array

Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
The following code


The o/p when u compile the above code is

b2[0][0][0][0] 1
b2[0][0][0][1] 2
b2[0][1][0][0] 0
b2[0][1][0][1] 0
b2[0][2][0][0] 0
b2[0][2][0][1] 0
b2[1][0][0][0] 0
b2[1][0][0][1] 0
b2[1][1][0][0] 0
b2[1][1][0][1] 0
b2[1][2][0][0] 0
b2[1][2][0][1] 0
2
ia[0][0]1
ia[0][1]0

In the above code ,the line b2[0][0]=b; assigns byte values 1,2 of b[][] array to b2[][][][] array.
But I wanna assign byte values 3,4 of b[][] array to b2[][][][] array.I am unable to do it....Can anybody try the code & tell me how to do it?
Thanks
Veena


SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Veena,
I'm not sure that I understand your question, but it sounds like you are saying that you can not access all of the elements of array b after it is assigned to array b2. In other words, it sounds like you can not access all of the elements of array b using the reference b2. I have not tried to run the code, but I think that your declaration of b2 is the problem. While the dimensions of array b are 2 X 2, the dimensions of array b2 are 2 X 3 X 1 X 2. You might have more luck accessing b[1][0] and b[1][2] using the reference b2 if array b2 were declared as a 2 X 3 X 2 X 2 array.
I hope that I correctly understood your question.


Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
Dan,
I am sorry if my question was not clear.In the above code byte values 1,2 of b array are assigned to b2 array.What should I do to assign byte values 3,4 of b array to b2 array.The above code produces following o/p.
b2[0][0][0][0] 1
b2[0][0][0][1] 2
b2[0][1][0][0] 0
b2[0][1][0][1] 0
b2[0][2][0][0] 0
b2[0][2][0][1] 0
b2[1][0][0][0] 0
b2[1][0][0][1] 0
b2[1][1][0][0] 0
b2[1][1][0][1] 0
b2[1][2][0][0] 0
b2[1][2][0][1] 0
2
ia[0][0]1
ia[0][1]0
What changes I should make in the above code to get the following o/p
b2[0][0][0][0] 3
b2[0][0][0][1] 4
b2[0][1][0][0] 0
b2[0][1][0][1] 0
b2[0][2][0][0] 0
b2[0][2][0][1] 0
b2[1][0][0][0] 0
b2[1][0][0][1] 0
b2[1][1][0][0] 0
b2[1][1][0][1] 0
b2[1][2][0][0] 0
b2[1][2][0][1] 0
2
ia[0][0]1
ia[0][1]0
I hope I made my doubt clear.
Dan Chisholm
Ranch Hand

Joined: Jul 02, 2002
Posts: 1865
Originally posted by Veena Point:

What changes I should make in the above code to get the following o/p
b2[0][0][0][0] 3
b2[0][0][0][1] 4

I have not tried to run the code, but my assumptions are as follows.
The values 3 and 4 are stored at
b2[0][0][1][0] 3
b2[0][0][1][1] 4
Since you have declared b2 as
byte b2[][][][]=new byte[2][3][1][2];
you won't have access to the elements where 3 and 4 are stored. You could solve the access problem by defining b2 as follows.
byte b2[][][][]=new byte[2][3][2][2];
If you want the declaration of b2 to remain unchanged and if you don't need access to the first two elements of b then you could try the following new assignment statement.
b2[0][0][0]=b[1];
As I mentioned earlier, I have not tried the code but I think that it will work.
I wish you luck.
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
Originally posted by Dan Chisholm:

If you want the declaration of b2 to remain unchanged and if you don't need access to the first two elements of b then you could try the following new assignment statement.
b2[0][0][0]=b[1];

Wow!It worked out.Thank you Dan.You really think very deep.This multi dimensional array problem was bugging me from many days.Now it is clear.Thanks alot.
Veena
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Question on multi dimensional array