aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes multi-dimensional array help 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 "multi-dimensional array help" Watch "multi-dimensional array help" New topic
Author

multi-dimensional array help

Jarlax King
Greenhorn

Joined: May 12, 2004
Posts: 3
I am trying to get a grasp on multi-dimensional arrays and am having a bit of trouble. Here is a question from the book that I would like some help understanding. The correct answer is a,b,e,f but I am not really getting why. Can anyone help me out?
1. public class Test {
2. public static void main (String [] args) {
3. byte [][] big = new byte [7][7];
4. byte [][] b = new byte [2][1];
5. byte b3 = 5;
6. byte b2 [][][][] = new byte [2][3][1][2];
7.
8. }
9. }
Which of the following lines of code could be inserted at line 7 and still allow the code to compile? (choose 4 that would work)
A. b2[0][1] = b;
B. b[0][0] = b3;
C. b2[1][1][0] = b[0][0];
D. b2[1][2][0] = b;
E. b2[0][1][0][0]=b[0][0];
F. b2[0][1]=big;
V Bose
Ranch Hand

Joined: Jul 10, 2003
Posts: 113
Originally posted by Jarlax King:
I am trying to get a grasp on multi-dimensional arrays and am having a bit of trouble. Here is a question from the book that I would like some help understanding. The correct answer is a,b,e,f but I am not really getting why. Can anyone help me out?
1. public class Test {
2. public static void main (String [] args) {
3. byte [][] big = new byte [7][7];
4. byte [][] b = new byte [2][1];
5. byte b3 = 5;
6. byte b2 [][][][] = new byte [2][3][1][2];
7.
8. }
9. }
Which of the following lines of code could be inserted at line 7 and still allow the code to compile? (choose 4 that would work)
A. b2[0][1] = b;
B. b[0][0] = b3;
C. b2[1][1][0] = b[0][0];
D. b2[1][2][0] = b;
E. b2[0][1][0][0]=b[0][0];
F. b2[0][1]=big;

You can look at multidimensional arrays as being an array of arrays. The left and right side of the assignment operator should be compatible.
Considering the answer choices,
-b2 is a four dimensional array. It needs 4 indices to effectively point to an element of type byte. Giving only 2 indices, points to a 2 Dimensional sub-array. b2[0][1] refers to a 2D array (same as b which is also a 2D array), which is why answer A is right.
-b[0][0] refers to a byte element, which is choice B
-b2[1][1][0] refers to a 1D array but b[0][0] on the RHS is a single byte element.This raises a compiler error.
- b2[1][2][0] again refers to a 1D array but b (on the RHS) is a 2D array. Causes a compiler error.
b2[0][1][0][0] points to a single byte element and is compatible with the RHS b[0][0], which also refers to a single byte element.
-b2[0][1] refers to a 2D array and is compatible with big, also a 2D array.
[ May 12, 2004: Message edited by: V Bose ]
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Let me try to help you out here. It's essential to determine what data type each variable refers to. Let's start with what's declared.
big is of type byte[][]
b is of type byte[][]
b3 is of type byte
b2 is of type byte[][][][]
Now, let's look at each of the possible answers. We'll start with A:
A. b2[0][1] = b;
The applicable question here is what is the type of b2[0][1]? Recall that b2 is a 4-dimensional array of bytes. Therefore, b[0] is a 3-dimensional array of bytes and b2[0][1] is a 2-dimensional array of bytes. The variable b is also of type byte[][] (a 2-dimensional array of bytes), so this assignment is valid. Choice "A" is correct.
B. b[0][0] = b3;
The variable b refers to a 2-dimensional array. Therefore, b[0] refers to a one-dimensional array and b[0][0] refers to a single byte. The variable b3 is also a single byte so this assignment is valid. Choice "B" is correct.
C. b2[1][1][0] = b[0][0];
The variable b2 refers to a 4-dimensional byte array. Therefore b[1] is a 3-dimensional array, b[1][1] is a 2-dimensional array, and b[1][1][0] is a 1-dimensional array. The variable b refers to a 2-dimensional array so b[0] is a 1-dimensional array and b[0][0] is just a byte. You can't assign a byte to an array of bytes so this line is invalid. Option "C" is not correct.
Hopefully, that'll help you along the way. I'll leave the last few for you to do. If you're still confused, let me know.
Corey


SCJP Tipline, etc.
Jarlax King
Greenhorn

Joined: May 12, 2004
Posts: 3
Thanks guys, I think I get it now! Below is my hard to understand attempt at visually explaining the logic.
What is being declared:
-----------------------
big is of type byte[][]
b is of type byte[][]
b3 is of type byte
b2 is of type byte[][][][]
The questions and answers:
--------------------------
A. b2[0][1] = b;
In this case, the result after the assignment would look something like this:
b2 [][reference var b][array b[] inserted] [byte value in b[][]inserted] - OK
B. b[0][0] = b3;
Here the result would be:
b[][byte value of b3] - OK
C. b2[1][1][0] = b[0][0];
Here:
attempt to assign byte value where byte array is expected, exception thrown
D. b2[1][2][0] = b;
Here:
b2[][][reference var b] [array b[] inserted] attempt to insert an array value where a byte is expected, exception thrown.
E. b2[0][1][0][0]=b[0][0];
Here:
b2[][][][byte value in b[][] inserted] - OK
F. b2[0][1]=big;
b2[][reference var big inserted][big[] inserted][big[][] inserted] - OK
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: multi-dimensional array help