Home > Software engineering >  Print 2D list in console in C#
Print 2D list in console in C#

Time:12-26

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?

CodePudding user response:

Print the values line by line:

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