Hi, Glen. In the following question, you are not required to construct arrays. You are required to recognize array type. Can 1-D array be type cast to 2D array? Can an integer be assigned to an array...... ?
Glen Iris wrote:
A- b2[1][1] = big;
B- b[1][0] = b3;
C- b2[0][1][1] = b;
D- b2[0[2][1] = b[1][0];
E- b2[1][1][0][1] = b[1][0];
F- b2[1][1] = b;
Answers: A,B,E and F are correct.
Explanation
A b2[1][1] is a 2D array , as well as big, so it can be assigned to big.
B. b[1][0] is a short, as well as b3. So, b[1][0] can be assigned to b3.
C. b2[0][1][1] is a 1D array, but b is a 2D array. So it cannot be assigned to b.
D. b2[0][2][1] is a 1D array, and it cannot be assigned to a short.
E. b2[1][1][0][1] is a short and can be assigned to b[1][0].
F. b2[1][1] is a 2D array and can be assigned to b.
I understand that it is not intuitive to have a 3D and 4D array.
Let me start from the basics.
Example 1:
1. short [][] s = new short[2][2]; is a 2D array.
2. s[1] is a 1D array. How do I know? It is a 2D array with 2[] 's and s[1] has one [], 2-1 = 1. Therefore, it is a 1D array.
Example 2:
1. short [][][] s = new short[2][2][2]; is a 3D array.
2. s[1] is a 2D array. How can I tell? It is a 3D array with 3[]'s and s[1] has one [], 3-1 = 2. Therefore, it is a 2D array.
Use analogy.
Imagine a 3D array short[][][] s is analogous to a building with several storeys. Each storey has several units (for each family). Each
unit has several rooms. If s = new short[5][5][5]. It is similar to a building with 5 storeys, with 5 units in each of the storeys and 5 rooms in each unit.
If you are asked what the 1st storey has, you will say a 2D array with : 5 units * 5 rooms/unit.
In geometry, there is only 1 D , 2 D and 3D, not 4 D or above. But you can think of domains instead of dimension.
If we have a short [5][5][5][5] s array, I can make an analogy to a street, with 5 buildings, each of them with 5 storeys, 5 units on each storey and 5 rooms in each unit. For example, if you are asked what street "s" has, you will say 5 buildings * 5 storey/building * 5 units/storey * 5 rooms/unit.
So, if the street is actually a 4D array, it has 4 domains (building*storey*unit*room).
So, a building is actually a 3D array, it has 3 domains (storey* unit*room) and etc.