How to create mutlidimensional list for this in C#?

17,729

Solution 1

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".

Solution 2

You should be able to work with it as you'd expect to deal with a list within a list.

matrix.Add(new List<string>);
matrix[0].Add("a string");

Solution 3

List<List<String>> matrix = new List<List<String>>();

foreach (String line in file)
{
   String[] values = line.Split(new Char[] { ' ' });

   matrix.Add(new List<String>(values));
}

Just iterate through your file and for every line do the following.

  1. Generate a new List
  2. Fill it with the data for the line
  3. Add the list for the current line to your list for the complete file

Note that foreach (String line in file) is just pseudo code. Further you can merge the to lines in the body to a single line.

matrix.Add(new List<String>(line.Split(new Char[] { ' ' })));

Solution 4

You're describing a jagged array. I'm not exactly sure if a List won't be overkill? If you just have to import the data from the file and than use it, a jagged array should be simple enough.

Example:

int[][] jaggedArray = new int[][] 
{
    new int[] {21, 22, 23, 33, 3323},
    new int[] {32, 32},
    new int[] {434433, 545454, 5454}
};

you can also buildup the jagged array in a loop while processing your file, like this:

int[][] result = new int[numberOfLines][];
for (int currentLine = 0; currentLine < numberOfLines; currentLine++)
{
    String line = fileStream.ReadLine();
    int[] values = SplitAndConvertLine(line);
    result[currentLine] = values;
}
Share:
17,729
user2120901
Author by

user2120901

Updated on July 06, 2022

Comments

  • user2120901
    user2120901 almost 2 years

    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;
    }
    
  • Daniel Brückner
    Daniel Brückner about 15 years
    table.Count() returns the number of rows.
  • Daniel Brückner
    Daniel Brückner about 15 years
    And if you will have to manipulate the data later, you will probably create a list again, because you need to add or remove something. So I absolutly prefer list because of the gained flexibility. And under the hood it is just a array with some smart resizing - not that much overhead.
  • Naveed
    Naveed about 15 years
    I'm offering it as a alternative, and already noted that it would not be wise to use them in case of changing data. Depending on the size of the data, it might be wise to know certain properties in advance, so the List can reserve a large capacity in advance.