Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to print 2D array to console in C#

I dont't have any code for this, but I do want to know how I could do this. I use visual studio 2010 C# if that matters.

Thanks

Jason

like image 599
kannoli Avatar asked Nov 27 '22 02:11

kannoli


2 Answers

    public static void Print2DArray<T>(T[,] matrix)
    {
        for (int i = 0; i < matrix.GetLength(0); i++)
        {
            for (int j = 0; j < matrix.GetLength(1); j++)
            {
                Console.Write(matrix[i,j] + "\t");
            }
            Console.WriteLine();
        }
    }
like image 72
Raz Megrelidze Avatar answered Dec 05 '22 18:12

Raz Megrelidze


you can print it all on one line

int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
Console.WriteLine(String.Join(" ", array2D.Cast<int>()));

output

1 2 3 4 5 6 7 8
like image 30
JJS Avatar answered Dec 05 '22 17:12

JJS