[英]c# - how to invert two dimensional array
我有一個執行長時間處理的程序。 第一步是將表格從 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")
我很樂意接受任何建議。
謝謝!
有一個被調用的方法,而不是table.getCol
和table.getRow
,然后調用它們。
就像是:
static bool IsColumns = true;
List<Cell> Get(string input)
{
if (IsColumns) return table.getCol(input);
else return table.getRow(input);
}
有一些不錯的代碼取自這個問題
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.