Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否将锯齿状字符[]转换为字符[],]?_C#_Arrays - Fatal编程技术网

C# 是否将锯齿状字符[]转换为字符[],]?

C# 是否将锯齿状字符[]转换为字符[],]?,c#,arrays,C#,Arrays,我昨天开始的一个“见鬼去吧”项目是翻译。我有它的大部分工作,除了边缘的情况 我变得懒惰,决定在befunge程序中阅读以下内容: char[][] program = File.ReadAllLines(args[0]).Select(x => x.ToCharArray()).ToArray(); 我知道以后我会为自己创作更多的作品,但我想去其他地方,就这样离开了。现在是晚些时候了,我需要修正program不是矩形的事实。假设我有一个befunge项目: v v <

我昨天开始的一个“见鬼去吧”项目是翻译。我有它的大部分工作,除了边缘的情况

我变得懒惰,决定在befunge程序中阅读以下内容:

char[][] program = File.ReadAllLines(args[0]).Select(x => x.ToCharArray()).ToArray();
我知道以后我会为自己创作更多的作品,但我想去其他地方,就这样离开了。现在是晚些时候了,我需要修正
program
不是矩形的事实。假设我有一个befunge项目:

v   v   <
    @
>       ^
v<
@
>       ^
第一行和第三行有9个字符长,但第二行只有5个字符。按照我设置befunge解释器的方式,我将在程序终止之前获得一个
索引自动边界异常
,因为在将
^
解释为方向改变后,我将尝试访问
程序[1][8]
,而
程序[1]
只有5长。我如何使用
program
创建一个
char[,]
,并用空格填充额外的字符,而不是试图捕获异常并围绕它跳舞


我知道我可以确定最长行的长度和行数,用这些行创建字符[,]并将它们复制过来,但我希望得到更简单、更优雅的东西。如果新方法更好,我完全可以扔掉上面的行。

您可以为它创建一个包装器,而不是重新创建整个锯齿数组(假设它可能相当大)。该包装器将能够执行边界检查,并在超出边界而不是出错时返回一些默认值

public class Matrix<T>
{
  public T[][] UnderlyingCollection {get;set;} //should probably be readonly and set in the constructor

  public T DefaultValue {get;set;}

  public T this[int i, int j]
  {
    get
    {
      if(UnderlyingCollection.Length > i && UnderlyingCollection[i].Length > j)
        return UnderlyingCollection[i][j];
      else
        return DefaultValue;
    }
    set
    { /*TODO implement*/ }

  }
}
公共类矩阵
{
public T[][]underyingcollection{get;set;}//可能应该是只读的,并在构造函数中设置
公共T默认值{get;set;}
公共T这[int i,int j]
{
得到
{
if(underyingcollection.Length>i&&underyingcollection[i].Length>j)
返回基础集合[i][j];
其他的
返回默认值;
}
设置
{/*TODO实现*/}
}
}

您可以为它创建一个包装器,而不是重新创建整个锯齿状数组(假设它可能相当大)。该包装器将能够执行边界检查,并在超出边界而不是出错时返回一些默认值

public class Matrix<T>
{
  public T[][] UnderlyingCollection {get;set;} //should probably be readonly and set in the constructor

  public T DefaultValue {get;set;}

  public T this[int i, int j]
  {
    get
    {
      if(UnderlyingCollection.Length > i && UnderlyingCollection[i].Length > j)
        return UnderlyingCollection[i][j];
      else
        return DefaultValue;
    }
    set
    { /*TODO implement*/ }

  }
}
公共类矩阵
{
public T[][]underyingcollection{get;set;}//可能应该是只读的,并在构造函数中设置
公共T默认值{get;set;}
公共T这[int i,int j]
{
得到
{
if(underyingcollection.Length>i&&underyingcollection[i].Length>j)
返回基础集合[i][j];
其他的
返回默认值;
}
设置
{/*TODO实现*/}
}
}

伙计,我不确定这是否是你要找的,但请检查一下:

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;

        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();

我知道这不是一个合适的基准测试,但看起来我的解决方案在性能方面并没有那么差。

伙计,我不确定这是否是你想要的,但看看这个:

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;

        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();

我知道这不是一个合适的基准测试,但看起来我的解决方案在性能方面并没有那么差。

基于@AndreCalil之前的答案,这可能会更有效,尤其是对于大型原始类型数组。基元类型的数组可以被视为字节的平面缓冲区,这在这类工作中很有用(如果您有汇编程序或C语言的经验):

static void Main(字符串[]args)
{
字符串[][]锯齿=新字符串[][]{新字符串[]{“alpha”,},
新字符串[]{“bravo”,“charlie”,},
新字符串[]{“delta”、“echo”、“foxtrot”,},
新字符串[]{“高尔夫”、“酒店”、“印度”、“朱丽叶”,},
新字符串[]{“基洛”、“利马”、“迈克”、“南希”、“奥斯卡”},
} ;
字符串[,]矩形=矩形阵列由锯齿状(锯齿状)组成;
返回;
}
公共静态T[,]矩形阵列自锯齿形(T[]a)
{
int行=a.长度;
int cols=a.Max(x=>x.Length);
T[,]值=新的T[行,列];
value.Initialize();
if(类型(T).i主)
{
int ELEMENTSIZEINOCETS=缓冲区.ByTeleLength(值)/value.Length;
对于(int i=0;i
基于@AndreCalil之前的回答,这可能会更有效,尤其是对于大型基元类型数组。基元类型的数组可以被视为字节的平面缓冲区,这在这类工作中很有用(如果您有汇编程序或C语言的经验):

static void Main(字符串[]args)
{
字符串[][]锯齿=新字符串[][]{新字符串[]{“alpha”,},