Representation of two-dimensional arrays in the memory

In summary: In Java, arr would be initialized in the same way (assuming there is enough memory):int arr [][] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}};In summary, arrays declared in Java are stored in memory row-major-wise or column-major-wise, depending on how elements are entered.
  • #1
Wrichik Basu
Science Advisor
Insights Author
Gold Member
2,116
2,691
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?
 
Technology news on Phys.org
  • #2
Wrichik Basu said:
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
 
  • Like
Likes Ibix
  • #3
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...
 
  • Like
Likes Wrichik Basu
  • #4
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.
 
  • #5
Wrichik Basu said:
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].
 

1. What is a two-dimensional array?

A two-dimensional array is a data structure that stores values in a grid with rows and columns. It is also known as a matrix.

2. How are two-dimensional arrays represented in memory?

Two-dimensional arrays are represented in memory as a sequential block of memory, with each element in the grid adjacent to the next one in the same row. This is known as row-major order.

3. How does the computer access elements in a two-dimensional array?

The computer uses the index of the row and column to access a specific element in a two-dimensional array. For example, arr[row][column].

4. Can a two-dimensional array have different data types in each row or column?

No, a two-dimensional array can only have one data type for all elements. However, you can create an array of arrays to store different data types in each row or column.

5. What are some advantages of using two-dimensional arrays?

Two-dimensional arrays are useful for storing and accessing data in a structured manner. They are also efficient for performing operations on rows and columns, such as sorting and searching.

Similar threads

  • Programming and Computer Science
Replies
29
Views
1K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
19
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
755
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top