Shiao Kung Chux wrote: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 code can insert into line 7 and compile successful (choise 4)
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. b[0][1][0][0] = b[0][0];
F. b2[0][1] = big;
accord to the text book , the answer is A,B,E,F.
i do not understand why C is false? if C is false why E is true?
why D is false? if D is false, why A is true?
I spent a lot of time figuring out how to handle multi dimensional arrays.
If you iterate over a one dimensional array assume int a[] = new int[3]; you refer to element as a[0],a[1] and a[2]
For two dimensional array int a[][] = int[2][2]; (initializing array) This represents a 2dimensional array. If you fix the first position.. what you receive is a one dimensional array.. which means a[0][] refers to first one dimensional array a[1][] refers to second one dimensional array since index starts from 0. a[0][0] represents a constant value .
Similarly for 3 dimensional array int a[][][] = new int[2][3][4]; If you fix the first position for eg: a[0][][] what you receive is a two dimensional array at first index (0).. a[1][][] means you are referring to a two dimensional array at 2nd index.
Visually you can imagine a 3D array as a rubix cube.. which is nothing but a collection of 2D arrays.. When you iterate over the the 3rd dimension for every iteration you refer to a plane which is a 2D array. Which means for (i=0;i<4;i++) {
// for every iteration below this you get a 2D array from these 2 inner loops)
for (j=0;j<4;j++) {
for (k=0;k<4;k++) {
}
}
}
for i=0 you refer to first 2 dimensional array.. Similarly for i =1 you refer to 2nd two dimensional array. a[0][j][k] a[1][j][k]
Now let us explore the answers one by one.
A)b2[0][1] = b means you have fixed first two indexes of 4 dimensional array b2[][][][] and this refers to two dimensional array and b also refers to a 2 d array. So you can assign a 2 d array to a 2d array.
b)b[0][0] = b3; b[0][0] refers to first row and first column of array and it can be any integer constant. b3 is also constant (can be assigned to another variable of same type)
c) b2[1][1][0]= b[0][0]; LHS refers to one dimensional array since first 3 positions are fixed from b2[][][][] and b[0][0] refers to a constant byte type . You cannot assign a byte type constant to a one dimensional byte array.
d)b2[1][2][0] = b;LHS is one dimensional array because first 3 positions are finalized..b is a 2D array. SO you cannot assign a 2d array into 1D array.
e)b[0][1][0][0] = b[0][0]; LHS refers to a constant value since all four positions are finalized. RHS also refers to a constant value. Hence correct.
f)b2[0][1] = big; LHS refers to a 2D array since only first positions are fixed from b2[][][][] and big also refers to 2D array. Hence a 2D array can be assigned to a 2D array.