Man, I'm not sure if this is what you're looking for, but check this out:
public static class CharArrayExtension
{
public static char[,] FormatMatrix(this char[][] matrix)
{
int TotalColumns = matrix.Length;
int TotalLines = 0;
//Get the longest line of the current matrix
for (int column = 0; column < TotalColumns; column++)
{
int line = matrix[column].Length;
if (line > TotalLines)
TotalLines = line;
}
//Instantiate the resulting matrix
char[,] Return = new char[TotalColumns, TotalLines];
Return.Initialize();
//Retrieve values from the current matrix
for (int CurrentColumn = 0; CurrentColumn < TotalColumns; CurrentColumn++)
{
int MaxLines = matrix[CurrentColumn].Length;
for (int CurrentLine = 0; CurrentLine < MaxLines; CurrentLine++)
{
Return[CurrentColumn, CurrentLine] = matrix[CurrentColumn][CurrentLine];
}
}
return Return;
}
}
Usage:
char[] Length5 = new char[]{ 'a', 'b', 'c', 'd', 'e'};
char[] Length10 = new char[10];
char[][] Matrix = new char[2][];
Matrix[0] = Length5;
Matrix[1] = Length10;
char[,] FormattedMatrix = Matrix.FormatMatrix();
Any feedback will be appreciated.
UPDATE
Nicholas pointed out the performance issue. I was curious about it, so I made the following micro-weak-benchmarking:
char[] Length5 = new char[]{ 'a', 'b', 'c', 'd', 'e'};
char[] Length10 = new char[10];
char[][] Matrix = new char[2][];
Matrix[0] = Length5;
Matrix[1] = Length10;
Stopwatch stopWatch = new Stopwatch();
stopWatch.Start();
for (int i = 0; i < 5000; i++)
{
char[,] FormattedMatrix = Matrix.FormatMatrix();
}
stopWatch.Stop();
Console.WriteLine(string.Format("Andre Calil: {0} ms", stopWatch.ElapsedMilliseconds));
stopWatch.Reset();
stopWatch.Start();
for (int i = 0; i < 5000; i++)
{
char[,] FormattedMatrix = RectArrayFromJagged<char>(Matrix);
}
stopWatch.Stop();
Console.WriteLine(string.Format("Nicholas Carey: {0} ms", stopWatch.ElapsedMilliseconds));
Console.ReadLine();
I've run it multiple times, and the average results was:
Andre Calil: 3 ms
Nicholas Carey: 5 ms
I know that this is not a proper benchmarking, but looke like my solution isn't so bad in terms of performance after all.