c#-如何反转二维数组

问题描述 投票:0回答:2

我有一个执行长时间处理的程序。第一步是将表从XML格式转换为二维数组(arr [,])。之后执行了许多步骤,只有在执行了这些步骤之后,我才知道表是具有行标题还是列标题。为了清楚起见,行标题表示的表如下:

Name    city   id
Anna     NY     1
Joe      NJ     2

列标题表示:

Name    Anna     Joe
City    NY       NJ
id      1        2

根据标题对表进行处理。我采用与某些标题相关的价值观并对其进行研究。我正在寻找一种以一种方式表示表格的方式,因此我不应该每次都检查表格类型是行还是列。我想避免以下代码:

List<Cell> cells;
if (tableType == rows)
  cells = table.getCol("Name");
else
  cells = table.getRow("Name")

我很乐意提出任何建议。

谢谢!

c# arrays rows
2个回答
0
投票

[This Question中有一些不错的代码

int[,] array = new int[4,4] {
    { 1,2,3,4 },
    { 5,6,7,8 },
    { 9,0,1,2 },
    { 3,4,5,6 }
};

int[,] rotated = RotateMatrix(array, 4);

static int[,] RotateMatrix(int[,] matrix, int n) {
    int[,] ret = new int[n, n];

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
             ret[i, j] = matrix[j, i];
        }
    }

    return ret;
}

0
投票

具有被调用的方法,而不是table.getColtable.getRow,然后调用它们。

类似:

static bool IsColumns = true;
List<Cell> Get(string input)
{
    if (IsColumns) return table.getCol(input);
    else return table.getRow(input);
}
© www.soinside.com 2019 - 2024. All rights reserved.