# Representation of two-dimensional arrays in the memory

• Java
Gold Member

## Main Question or Discussion Point

Arrays declared in java are stored in the memory in two ways: row-major-wise and column-major-wise.

As per our teacher, the choice of the storage technique depends on how we enter the array elements. For example, for this code:

Java:
int arr[][] = new int[10][10];
Scanner kb = new Scanner (System.in);
for (int i = 0; i < 10; i++){
for (int j = 0; j < 10; j++){
arr[i][j] = kb.nextInt();
}
}
The array is stored in the memory in row-major-wise because I'm entering elements in the array row by row.

But in the following code:

Java:
int arr[][] = new int[10][10];
Scanner kb = new Scanner (System.in);
for (int i = 0; i < 10; i++){
for (int j = 0; j < 10; j++){
arr[j][i] = kb.nextInt();
}
}
It is evident that I'm entering elements column-wise, so the array would be stored in the memory in column-major-wise.

But consider this code:

Java:
int arr[][] = new int[10][10];
arr[5][6] = 9;
arr[7][2] = 67;
Here, I'm inserting elements in the array in a random manner. So, how will this array be stored in the memory: row-major-wise or column-major-wise?

Related Programming and Computer Science News on Phys.org
DavidSnider
Gold Member
Arrays declared in java are stored in the memory in two ways: row-major-wise and column-major-wise.

As per our teacher, the choice of the storage technique depends on how we enter the array elements. For example, for this code:

Java:
int arr[][] = new int[10][10];
Scanner kb = new Scanner (System.in);
for (int i = 0; i < 10; i++){
for (int j = 0; j < 10; j++){
arr[i][j] = kb.nextInt();
}
}
The array is stored in the memory in row-major-wise because I'm entering elements in the array row by row.

But in the following code:

Java:
int arr[][] = new int[10][10];
Scanner kb = new Scanner (System.in);
for (int i = 0; i < 10; i++){
for (int j = 0; j < 10; j++){
arr[j][i] = kb.nextInt();
}
}
It is evident that I'm entering elements column-wise, so the array would be stored in the memory in column-major-wise.

But consider this code:

Java:
int arr[][] = new int[10][10];
arr[5][6] = 9;
arr[7][2] = 67;
Here, I'm inserting elements in the array in a random manner. So, how will this array be stored in the memory: row-major-wise or column-major-wise?
Java multidimensional arrays are arrays of arrays and are not necessarily stored contiguously

Ibix
Ibix
Generally speaking you can regard an array arr[M][N] as M one dimensional arrays, each of length N. But interpretation is up to you, or whatever library you are using. Say you have the matrix $$\left (\begin {array}{ccc}1&2&3\\4&5&6\\7&8&9\end {array}\right)$$then you can store it in column-major or row-major order with either of your programs by entering 1/2/3/4/5/6/7/8/9 or 1/4/7/2/5/8/3/6/9. The question is, does your matrix multiplication function regard the first index as the row index or column index? It can do either. Make sure you know.

So the answer to your question is caveat emptor. Or caveat programmor, I suppose. Always read the docs/comments to see how the programmer thought they were stored. And do a test so you know how the computer thinks they were stored...

Wrichik Basu
jedishrfu
Mentor
A one dim array of size n is a contiguous block of n elements. Each element is either a pointer to an instance of a data type , or the contents of a primitive java data type like Boolean, int, long, float, or double.

Multiple dim arrays are organized as array elements that point to other arrays. The java compiler handles the details of referencing within a multiple dim array. This scheme allows for sparse arrays to not take up memory than needed.

The Java JNI manual can explain this more detail as programmers interested in interfacing Java with C need to understand how it works.

Mark44
Mentor
Arrays declared in java are stored in the memory in two ways: row-major-wise and column-major-wise.
I think that you are confusing two ideas -- how elements of a two-dimensional array are stored in Java, and how such arrays can be initialized. If we limit the discussion to rectangular two-dimensional arrays in Java, I believe that they are stored in the same way as would C or C++ and many other programming languages. Consider this example in C:
C:
int arr [][3] = { {1, 2, 3},
{4, 5, 6},
{7, 8, 9}};
The first three values, 1, 2, and 3, would be stored in the first three memory locations of the memory allocated for arr. The values 4, 5, and 6 would be in the next three memory locations, and the values 7, 8, and 9 would be in the last three memory locations.

I believe that Java would store an array declared like this in exactly the same way; i.e., in row-major order.

You could declare an empty array, and then initialize it row by row or column by column, but that has nothing to do with how the array is actually stored. It's just how you are initializing the array.

In contrast to C, C++, C+, Python, etc., Fortran stores the elements of a two dimensional array in column major order. For an array similar to my example above, but declared in a Fortran program, the values 1, 4, and 7 would be in the first three memory locations, the values 2, 5, and 8, would be in the next three locations, and the values 3, 6, and 9 would be in the last three memory locations.
Wrichik Basu said:
As per our teacher, the choice of the storage technique depends on how we enter the array elements. For example, for this code:

Java:
int arr[][] = new int[10][10];
Scanner kb = new Scanner (System.in);
for (int i = 0; i < 10; i++){
for (int j = 0; j < 10; j++){
arr[i][j] = kb.nextInt();
}
}
The array is stored in the memory in row-major-wise because I'm entering elements in the array row by row.
No. How you put the values in the array has nothing to do with how they are actually stored.
Wrichik Basu said:
But in the following code:

Java:
int arr[][] = new int[10][10];
Scanner kb = new Scanner (System.in);
for (int i = 0; i < 10; i++){
for (int j = 0; j < 10; j++){
arr[j][i] = kb.nextInt();
}
}
It is evident that I'm entering elements column-wise, so the array would be stored in the memory in column-major-wise.

But consider this code:

Java:
int arr[][] = new int[10][10];
arr[5][6] = 9;
arr[7][2] = 67;
Here, I'm inserting elements in the array in a random manner. So, how will this array be stored in the memory: row-major-wise or column-major-wise?
Again, how you initialize the array has nothing to do with the terms row-major ordering and column-major ordering.
You can assign values to the array in any order you want, but this won't affect where they are location in memory. arr[5][6] will come just before arr[5][7] in memory. In Fortran, or other languages that store two-dimensional arrays in column-major order, arr[5][6] would come just before arr[6][6].