Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reg :- Java Multi Dimension Array

 
RAGU KANNAN
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
RAGU KANNAN
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic