| Author |
Reg :- Java Multi Dimension Array
|
RAGU KANNAN
Ranch Hand
Joined: Dec 16, 2005
Posts: 103
|
|
I couldn't undersand the answer for the following questions. Why the anser "C & D " are wrong? Question :- Given the following, 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 four 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; Answer:- 12. A, B, E, and F. This question covers the issue of, �What can I assign to an array reference variable?� The key is to get the dimensions right. For example, if an array is declared as a two-dimensional array, you can�t assign a one-dimensional array to a one-dimensional array reference. C is wrong because it tries to assign a primitive byte where a byte array (one dimension) is expected. D is wrong because it tries to assign a two-dimensional array where a one-dimensional array is expected.
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
It tells you why c and d are wrong at the bottom of the answer:
C is wrong because it tries to assign a primitive byte where a byte array (one dimension) is expected. D is wrong because it tries to assign a two-dimensional array where a one-dimensional array is expected.
So what's not clear?
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
RAGU KANNAN
Ranch Hand
Joined: Dec 16, 2005
Posts: 103
|
|
Thanks for the reply Mr.Barry. If b[0][0] it is two dimentision array like that b2[1][1][0] is three dimentision array. but answer says one dimention array, how it's one dimentision array? C. b2[1][1][0] = b[0][0]; D. b2[1][2][0] = b; Thanks, Raghu.K
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
b2 is a "4-dimensional" byte array, instantiated with the dimensions [2][3][1][2]. In Java, multi-dimensional arrays are really just arrays of arrays. In particular, for int values w, x, y, and z within range, b2[w] represents a 3-d array, b2[w][x] represents a 2-d array, b2[w][x][y] represents a 1-d array, and b2[w][x][y][z] represents a byte.
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
 |
|
|
subject: Reg :- Java Multi Dimension Array
|
|
|