- #1
- 2,136
- 2,712
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:
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:
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:
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?
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?