我已经看过关于旋转2D阵列的其他帖子,但它并不是我想要的.我想要这样的东西
int[,] original= new int[4,2]
{
{1,2},
{5,6},
{9,10},
{13,14}
};
Run Code Online (Sandbox Code Playgroud)
我想把它变成这样,rotateArray = {{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;
}
Run Code Online (Sandbox Code Playgroud)
有没有一种简单的方法来迭代c#中的列?
乙
如果将阵列更改为阵列数组,则会更容易.如果你把它改成int [] [],我发现了这个:
int[][] original = new[]
{
new int[] {1, 2},
new int[] {5, 6},
new int[] {9, 10},
new int[] {13, 14}
};
Run Code Online (Sandbox Code Playgroud)
和旋转方法:
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;
}
Run Code Online (Sandbox Code Playgroud)