简体   繁体   English

在 C# 的控制台中打印二维列表

[英]Print 2D list in console in C#

I have following code.我有以下代码。

class Solution
{
    static void Main(String[] args)
    {
        var matrix = new List<List<int>>();
        for (int i = 0; i < 6; ++i)
        {
            string[] elements = Console.ReadLine().Split(' ');
            matrix.Add(new List<int>());
            foreach (var item in elements)
            {
                matrix[i].Add(int.Parse(item));
            }
        }
    }
}

I know to print out the array which we read from console, convert it to int from string, we will have to use foreach loop.我知道要打印出我们从控制台读取的数组,将它从字符串转换为 int,我们将不得不使用 foreach 循环。 But here to print out the list in the console how can we write the code?但是这里要在控制台打印出列表,我们怎么写代码呢?

Print the values line by line:逐行打印值:

foreach (var line in matrix)
{
  foreach (var item in line)
  {
    Console.Write(item+"\t");
  }
  Console.WriteLine();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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