How is a two-dimensional array represented in memory in java?
20:06 13 Dec 2014

I am trying to implement a data structure with Key, Value pairs and was looking at array implementations.

One way of achieving this is to declare seperate 1-D arrays for Key and Values.

  private int[] keys = new int[N];
private int[] values = new int[N];

But can the same be achieved by declaring a 2-D array as follows and not compromise on data locality?

private int[][] keysAndValues = new int[2][N];

It seems important here that Java implements multidimensional arrays in row-major order? Are there any performance advantages in declaring the array this way, or does this make the code less readable?

java arrays multidimensional-array