How can you compare int[0][0] and int[0][1] ... int[0][n] ?

Using:

 Arrays.deepEquals 

getting

 System.out.println(Arrays.deepEquals(new int[0][0],new int[0][3])); output : true 

When comparing arrays of type int[i][j] , where i != 0 , using Arrays.deepEquals everything is ok. But there is a need to compare arrays, where i == 0 . I would be grateful for the help.

  • one
    I do not understand what the trouble is, call the method and get the result. Another thing is that this is not entirely logical; new int [0] [3] is an empty array in which nothing can be written and compared, respectively. - Artem Konovalov
  • I just had a method in which I tried to transfer the coordinates of the shot using an array and compared it with the coordinates of the field. public void doShoot (int [] [] shootPoints) {for (int i = 0; i <10; i ++) {for (int j = 0; j <10; j ++) {if (Arrays.deepEquals (new int [i ] [j], shootPoints)) {if (Field.cells [i] [j] == Field.Type.EMPTY || Field.cells [i] [j] == Field.Type.AMBIENT_AREA) {Field.cells [i] [j] = Field.Type.ALREADY_SHOOTED; ..... As I understand it, it was absolutely wrong. It is necessary to alter the program using Point (x, y), for example. Thanks for the help. - Sasha

1 answer 1

There is an assumption that you incorrectly use two-dimensional arrays. You are really in glory for the performance you can store the coordinates in arrays, but for 2D, a rather ordinary array of two elements is enough, where coord [0] is x, coord [1] is y. In this case, the set of shots will be a two-dimensional array (array of arrays) where arr [0] [0] is the x-coordinate of the 1st shot, arr [1] [1] is the y-coordinate of the second shot, etc.