男人,我不知道如果这是你在找什么,但检查了这一点:
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;
}
}
用法:
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();
任何反馈将不胜感激。
UPDATE
尼古拉斯指出的性能问题。我很好奇,所以我做了如下的微弱的标杆:
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();
我已经多次运行它,平均结果是:
Andre Calil: 3 ms
Nicholas Carey: 5 ms
我知道那这不是一个适当的基准测试,但像我的解决方案一样,在性能方面并没有那么糟糕。
我觉得你的回答你的问题在那里:“只需确定最长行的长度,行数,创建char [,]并将它们复制到” – Almo 2012-08-17 17:59:00
“哦,以及制作Befunge解释器的道具。这是一个很酷的esolang。我做了一个类似于那么多年前的冲击波游戏,在那里编写了像Carnage Heart这样的机器人。 – Almo 2012-08-17 17:59:50
只有一个解决方案并不能使其成为最佳解决方案。如果有人能够以不同的方式指向我,我可以学习更多关于C#的知识。 – 2012-08-17 18:00:24