微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

C#,旋转2D数组

我看过关于旋转2D阵列的其他文章,但它并不是我想要的.
我想要这样的东西
int[,] original= new int[4,2]
       {
           {1,2},{5,6},{9,10},{13,14}
       };

我想把它变成这样,
rotatingArray = {{1,5,9,13},{2,6,10,14}};
我希望按列进行一些分析,而不是按行进行分析.

这有效,但有更简单的方法吗?

private static int[,] RotateArray(int[,] myArray)
  {
        int org_rows = myArray.GetLength(0);
        int org_cols = myArray.GetLength(1);

        int[,] myRotate = new int[org_cols,org_rows];

        for (int i = 0; i < org_rows; i++)
        {
            for(int j = 0; j < org_cols; j++)
            {
                myRotate[j,i] = myArray[i,j];
            }
        }

        return myRotate;
    }

有没有一种简单的方法来迭代c#中的列?

解决方法

如果将阵列更改为阵列数组,则会更容易.如果你把它改成int [] [],我发现了这个:
int[][] original = new[]
                                   {
                                       new int[] {1,new int[] {5,new int[] {9,new int[] {13,14}
                                   };

和旋转方法

private static int[][] Rotate(int[][] input)
{
    int length = input[0].Length;
    int[][] retVal = new int[length][];
    for(int x = 0; x < length; x++)
    {
        retVal[x] = input.Select(p => p[x]).ToArray();
    }
    return retVal;
}

原文地址:https://www.jb51.cc/csharp/100426.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐