The algorithm is simple: scroll through the array and create a new one-dimensional.
using System;
class MainClass {
public static void Main (string[] args) {
int[,] arrayBidimensional = new int[3, 3]
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
var linhas = arrayBidimensional.GetLength(0);
int colunas = arrayBidimensional.GetLength(1);
var novoArray = new int[linhas * colunas];
for(int i = 0; i < linhas; i++) {
for(int j = 0; j < colunas; j++) {
var indice = colunas * i + j;
novoArray[indice] = arrayBidimensional[i, j];
Console.WriteLine($"{indice} = {novoArray[indice]}");
}
}
}
}
See working on Repl.it
On second thought, the above code can still be simplified to
using System;
class MainClass {
public static void Main (string[] args) {
int[,] arrayBidimensional = new int[3, 3]
{
{ 1, 2, 3 },
{ 4, 5, 6 },
{ 7, 8, 9 }
};
var novoArray
= new int[arrayBidimensional.GetLength(0) * arrayBidimensional.GetLength(1)];
int indice = 0;
foreach(var item in arrayBidimensional) {
novoArray[indice] = item;
Console.WriteLine($"{indice} = {novoArray[indice]}");
indice++;
}
}
}
See working on Repl.it
The simplest way is to traverse the 2 dimensions of the array (with a for), and assign the values to a second array.
– António Campos
António Campos, you can illustrate?
– sYsTeM