Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create mutlidimensional list for this in C#?

I got a table (in file) which I split into blocks by spaces.

I need structure like this:

-----------------------------
|21|22|23|33|3323|
|32|32|
|434433|545454|5454|
------------------------------

It's more like each row is its own table. How should I do this?

I tried List<List<string>> matrix = new List<List<string>>(); but I can't seem to find a way to work with it.

EDIT - can someone tell me what's wrong with this code???? Matrix[0][0] is same as matrix [1][0].. it seems that same row is added to matrix all the time, but I clear it ...

static ArrayList ReadFromFile(string filename)
    StreamReader SR;
    string S;
    string[] S_split;

    SR = File.OpenText(filename);
    S = SR.ReadLine();

    ArrayList myItems = new ArrayList();

    List<List<string>> matrix = new List<List<string>>();
    List<string> row = new List<string>();

    while (S != null)
    {
        row.Clear();
        S_split = S.Split(' ');
        for (int i = 1; i < S_split.GetLength(0); i++)
        {
            row.Add(S_split[i]);
            matrix.Add(row);
        }              

        S = SR.ReadLine();
    }
    Console.WriteLine(matrix[1][1]);
    SR.Close();
    return myItems;
}
like image 624
Skuta Avatar asked Dec 30 '22 02:12

Skuta


1 Answers

Not sure if I understand this correctly.

        List<List<int>> table = new List<List<int>>();
        List<int> row = new List<int>();
        row.Add(21);
        row.Add(22);
        row.Add(23);
        row.Add(33); // and so on
        table.Add(row);

        row = new List<int>();
        row.Add(1001);
        row.Add(1002);
        table.Add(row);

        MessageBox.Show(table[0][3].ToString());

The program should show a message box with text "33".

like image 74
Gant Avatar answered Jan 01 '23 15:01

Gant