This is very interesting question "How to represents 2D array into 1D array?". In this post I'll show you two ways to do it:
- Row-Major order
- Column-Major order (vectorization)
Reasons to use it:
- When you transfer 2D array using file between applications written in different languages.
- When you are traversing array you will have better performance, because accessing the element from contiguous order is usually faster than accessing element from 2D array.
Examples
2D Example
Now I'll give you a short example with 2D array and how it's represents contiguously in memory:
int[,] my2dArray = { {1, 2, 3}, {4, 5, 6} };
The result will be:
1, 2, 3
4, 5 ,6
We have 6 numbers separated in 2 rows (3 numbers per row).
If you want to use row-major order or column-major order you must to know the exact width and height of array, because the length of array will be width * height For example:
int[] myArray = new int[rowsNumber * columnsNumber];
Row-Major order
I use this way for representing two dimensional array. This is the formula for row-major order:
Element = currentRow * columnsNumber + currentColumn
And this is simple example:
int rowsNumber = 2, columnsNumber = 3;
int[,] my2DArray = new int[rowsNumber, columnsNumber];
int[] myArray = new int[rowsNumber * columnsNumber];
for (int currentRow = 0; currentRow < rowsNumber; currentRow++)
{
for (int currentColumn = 0; currentColumn < columnsNumber; currentColumn++)
{
my2DArray[currentRow , currentColumn] = int.Parse(Console.ReadLine());
}
}
for (int currentRow = 0; currentRow < rowsNumber; currentRow++)
{
for (int currentColumn = 0; currentColumn < columnsNumber; currentColumn++)
{
myArray[currentRow * columnsNumber + currentColumn] = my2DArray[currentRow, urrentColumn];
int value = myArray[currentRow * columnsNumber + currentColumn];
Console.WriteLine(value);
}
}
Now in the memory there will be only one row with 6 elements (columns) not 2 rows with 3 elements (columns) per row.
The result is: 1, 2, 3, 4, 5, 6
*Input data: 1 2 3 4 5 6
Column-Major order (vectorization)
This type of representing is more popular as "vectorization", because you make all columns from the array to be 1D array (vector). This is the formula for column-major order:
Element = currentColumn * rowsNumber + currentRow
int rowsNumber = 2, columnsNumber = 3;
int[,] my2DArray = new int[rowsNumber, columnsNumber];
int[] myArray = new int[rowsNumber * columnsNumber];
for (int currentRow = 0; currentRow < rowsNumber; currentRow++)
{
for (int currentColumn = 0; currentColumn < columnsNumber; currentColumn++)
{
my2DArray[currentRow , currentColumn] = int.Parse(Console.ReadLine());
}
}
for (int currentRow = 0; currentRow < rowsNumber; currentRow++)
{
for (int currentColumn = 0; currentColumn < columnsNumber; currentColumn++)
{
myArray[currentColumn * rowsNumber + currentRow] = my2DArray[currentRow, urrentColumn];
int value = myArray[currentColumn * rowsNumber + currentRow];
Console.WriteLine(value);
}
}
Now in the memory there will be only one row with 6 elements (columns).
The result is: 1, 4, 2, 5, 3, 6
*Input data: 1 2 3 4 5 6