-1

I have a program which performs a long processing. The first step is converting table from XML format to 2 dimensional array (arr[,]). After that many steps are perfomed and only after them I know if the table has row title or column title. To make it clear, row title means table like:

Name    city   id
Anna     NY     1
Joe      NJ     2

column title means:

Name    Anna     Joe
City    NY       NJ
id      1        2

The table is processed according to the title. I take to values related to some title and work on them. I'm looking for a way to represent the table in one way so I shouldn't check each time if the table type is rows or columns. I want to avoid something like to following code:

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

I would be happy to any suggestion.

Thanks!

mayap
  • 569
  • 5
  • 19
  • if you have an 2D array you'll have probably a nested for loop. Inverse the loops. Or clarify your question. – Steve B Jul 24 '12 at 12:34
  • 1
    What have you tried? This should be some simple code with loops and a little index calculation. – O. R. Mapper Jul 24 '12 at 12:34
  • 1
    What you need is called Pivoting. http://stackoverflow.com/questions/3687708/pivoting-a-collection-of-arrays – Alexander Efimov Jul 24 '12 at 12:34
  • Does this answer your question? [How do you rotate a two dimensional array?](https://stackoverflow.com/questions/42519/how-do-you-rotate-a-two-dimensional-array) –  Dec 10 '20 at 12:54

2 Answers2

0

Have a method that's called instead of table.getCol and table.getRow and then calls them.

Something like:

static bool IsColumns = true;
List<Cell> Get(string input)
{
    if (IsColumns) return table.getCol(input);
    else return table.getRow(input);
}
ispiro
  • 26,556
  • 38
  • 136
  • 291
-1

There some nice code taken from 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;
}
Community
  • 1
  • 1
John Mitchell
  • 9,653
  • 9
  • 57
  • 91