-1
  1. int[][][] a = new int[3][3][5];
  2. int [][][] b = new int[2][][]; b[0] = new int[2]; // here why we get error. in the above code how java consist each braces.

in java how the above code will allocate memory in concept of array of array.

Jens
  • 67,715
  • 15
  • 98
  • 113

2 Answers2

0

The array int [][][] b = new int[2][][]; is an array of array of array. So b[0] is an array of array. You are only allocating memory for the first dimension and not the second dimension so you're getting an error. Try b[0] = new int[2][];

Mohammad
  • 187
  • 1
  • 12
0

here why we get error. in the above code how java consist each braces.

int [][][] b = new int[2][][];   //b is an array of (array of (array of int))
b[0] = new int[2];               //b[0] is an (array of (array of int))

You are assigning int[2] is only an (array of int) to b[0], hence giving you the error.

b[0] = new int[2][];             //assign (array of (array of int)) to b[0] --> OK

In short, b[0] is expecting a 2D array, and you are currently assigning it a 1D array and that is causing the error.

user3437460
  • 17,253
  • 15
  • 58
  • 106