Converting a C# Two-Dimensional Array to a One-Dimensional Array
C# Array Dimension Conversion Operations
Hello everyone, in this article I will explain how you can convert between one-dimensional and two-dimensional arrays. For those of you who wonder why we might need something like this, let me briefly summarize: I have been working on a Tic-Tac-Toe game for the past two days and I used Firebase as the online infrastructure.
Within Firebase, I was thinking of using Firestore, but unfortunately Firestore does not support multi-dimensional arrays, and it was saving the data as a one-dimensional array only. However, my data structure was multi-dimensional (2D), and therefore I needed such a conversion.
Anyway, without making it too long and wasting your time, let's see how we can do this process.
Note: You can edit the codes below yourself and adapt them for larger arrays.
Converting a Two-Dimensional Array to a One-Dimensional Array
public static T[] ConvertToArray<t>(T[,] matrix)
{
var array = matrix.Cast<t>().ToArray(); // With the help of LINQ, we convert our matrix into an array.
return array; // We return the result.
}
</t></t>
Converting a One-Dimensional Array to a Two-Dimensional Array
public static T[,] ConvertToMatrix<t>(T[] array, int lenght)
{
if (array.Length != lenght * lenght) // We check the width of the array, will it be compatible with the matrix?
Console.WriteLine("Invalid Lenght"); // If not compatible, we output an error message.
var matrix = new T[lenght, lenght]; // We create our matrix.
var counter = 0; // We create a counter to navigate inside the array.
// The loop in which we perform the process of converting the array to the matrix.
for(var y = 0; y < lenght; y++)
{
for(var x = 0; x < lenght; x++)
{
matrix[y, x] = array[counter];
counter++;
}
}
return matrix; // We return the result.
}
</t>
All Code
using System;
using System.Linq;
namespace TestProject
{
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("----- Array To Matrix Start -----");
// We create an array for testing.
int[] numberArray = new int[9]
{
0,1,2,3,4,5,6,7,8
};
// We convert our array into a matrix.
var numberConvertedMatrix = ConvertToMatrix<int>(numberArray, 3);
// We print the result to the screen.
for(int y = 0; y < 3; y++)
{
for (int x = 0; x < 3; x++)
{
Console.Write($"{numberConvertedMatrix[y, x]}");
}
Console.Write("\n");
}
// Here we print the type of numberConvertedMatrix, i.e., our matrix variable, to the screen—it gives us int[,].
Console.WriteLine($"{numberConvertedMatrix}");
Console.WriteLine("----- Array To Matrix Finish -----");
Console.WriteLine("----- Matrix To Array Start -----");
// We create a matrix.
int[,] numberMatrix = new int[3, 3]
{
{0,1,2 },
{3,4,5 },
{6,7,8 }
};
// We convert our matrix into an array. Where it says ConvertToArray<int>, you can write any variable type you want instead of <int> and use this function with that data type.
var numberConvertedArray = ConvertToArray<int>(numberMatrix);
// We print the result to the screen
foreach(var number in numberConvertedArray)
{
Console.WriteLine($"{number}");
}
// Here we print the type of numberConvertedArray, i.e., our array variable, to the screen—it gives us int[].
Console.WriteLine($"{numberConvertedArray}");
Console.WriteLine("----- Matrix To Array Finish -----");
}
public static T[,] ConvertToMatrix<t>(T[] array, int lenght)
{
if (array.Length != lenght * lenght) // We check the width of the array, will it be compatible with the matrix?
Console.WriteLine("Invalid Lenght"); // If not compatible, we output an error message.
var matrix = new T[lenght, lenght]; // We create our matrix.
var counter = 0; // We create a counter to navigate inside the array.
// The loop in which we perform the process of converting the array to the matrix.
for(var y = 0; y < lenght; y++)
{
for(var x = 0; x < lenght; x++)
{
matrix[y, x] = array[counter];
counter++;
}
}
return matrix; // We return the result.
}
public static T[] ConvertToArray<t>(T[,] matrix)
{
var array = matrix.Cast<t>().ToArray(); // With the help of LINQ, we convert our matrix into an array.
return array; // We return the result.
}
}
}
</t></t></t></int></int></int></int>


Yorum Gönder