-1

Can anyone help me to understand the number of objects created with the below snippet:

int[][] a= new int[4][3];  //with this line first.

And, secondly if I re-initialize a with:

a = new int[3][7]; //finally with this line also.

Also, let me know if there is any tool to check the number of objects created in my complete application.

triandicAnt
  • 1,328
  • 2
  • 15
  • 40
alok
  • 53
  • 1
  • 7

1 Answers1

5

Number of arrays in multiple dimensions will be 1 for the array + 1 * size of first sub array size + 1 * size of first sub array size * size of second sub array size and so on... exclude the last dimension which contain the integer values

etc:

a = int[4][3] // 1 + 1 * 4 = 5 arrays
b = int[3][7] // 1 + 1 * 3 = 4 arrays
c = int[5][6][7] // 1 + 1 * 5 + 1 * 5 * 6 = 36 arrays
Roy Shmuli
  • 4,979
  • 1
  • 24
  • 38
  • So there will be 1 object for each array in multi dimensional array. Hence a = new int[4][3] will give 5 objects, right? – alok Mar 11 '18 at 08:11
  • I don't understand the meaning of 1 object for each array. In a = new int[4][3], your pointer a is array of arrays, size 4. Each one of them is array of int[3]. So it will be 5 as you wrote – Roy Shmuli Mar 11 '18 at 09:13
  • As in the question I was asking about the number of array objects created, hence I used the term 'object'. – alok Mar 13 '18 at 14:36