简体   繁体   中英

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

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