简体   繁体   中英

C# multi dimensional list

this is my current working code below.

List<double> values = new List<double>();

foreach (string row in rows)
{
   Print(row);
                    
   //Checking if the row contains 4 numbers after the " ".
   //If true, then added to the collection
   if (Regex.IsMatch(row, @" (\d{4})"))
      values.Add(double.Parse(Regex.Match(row, @" (\d{4})").Groups[1].Value));
}

foreach (double i in values) 
{
   Print("row value " + i);
}   

What I would also like to do is also parse the 4 numbers before the " " and when looping through each row as above however have 2 variables.

I have attempted to solve this problem by searching on the web and the closest outcome I got was as follows but I don't know how to loop through?

List<string[]> grid = new List<string[]>();

//Populate the list
foreach (var line in lines) grid.Add(line.Split(' '));

//Print(grid);
                
//You can still access it like your 2D array:
Print(grid[0][0] + " " + grid[0][1]); 

Thanks in advance for your help/suggestions.

Something like this?

foreach(var array in grid)
{
  Print(array[0] + " " + array[1]); 
}

I've node added any safety checks, you need to check that there are enough elements you want, otherwise you'd get an index out of range exception

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM