首页 > 解决方案 > C#多维列表

问题描述

这是我当前的工作代码。

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);
}   

我还想做的是解析“”之前的4个数字,并且在如上所述遍历每一行时有2个变量。

我试图通过在网上搜索来解决这个问题,我得到的最接近的结果如下,但我不知道如何循环?

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]); 

提前感谢您的帮助/建议。

标签: c#list

解决方案


像这样的东西?

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

我已经节点添加了任何安全检查,您需要检查是否有足够的元素需要,否则您会得到索引超出范围异常


推荐阅读