How to print 2D array to console in C#

后端 未结 10 1595
野性不改
野性不改 2020-12-06 22:59

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

相关标签:
10条回答
  • 2020-12-06 23:24

    Below is multiple solution to how to implement multidimentional array, I think it is pretty straight forward in c#.

    using System;
    using System.Collections.Generic;
    
    namespace DataStructure
    {
        class Program : SortedZeros
        {
            static void Main(string[] args)
            {
                // Two-Dimensional Array
                int[,] numbers2D = new int[3, 2] 
                { 
                    { 9, 99 }, 
                    { 3, 33 }, 
                    { 5, 55 }
                };
    
                // 3 * 3
                int[,] dataTest2D = new int[3, 3] 
                { 
                    {3, 5, 7}, 
                    {4, 3, 8},
                    {9, 6, 9},
                };
    
                // A similar array with string elements.
                int[,] matrix = new int[4, 4]
                {
                     {1, 2, 3, 6},
                     {4, 5, 6, 4},
                     {7, 8, 9, 6},
                     {7, 8, 9, 2},
                };
    
                int rowLength = matrix.GetLength(0);
                int colLength = matrix.GetLength(0);
    
                for (int i = 0; i < rowLength; i++)
                {
                    for (int j = 0; j < colLength; j++)
                    {
                        Console.Write(string.Format("{0} ", matrix[i, j]));
                    }
                    Console.Write(Environment.NewLine + Environment.NewLine);
                }
    
                // using foreach to print out the 2 * 2 in one straightline
                foreach (int i in numbers2D)
                {
                    Console.Write("{0} ", i);
                }
                Console.WriteLine();
    
                Console.WriteLine();
                for (int i = 0; i < dataTest2D.GetLength(0); i++)
                {
                    for (int j = 0; j < dataTest2D.GetLength(1); j++)
                    {
                        Console.Write(string.Format("{0} ", dataTest2D[i, j]));
                    }
                    Console.Write(Environment.NewLine + Environment.NewLine);
                }
            }
    
        }
    }
    
    0 讨论(0)
  • 2020-12-06 23:29
    int[,] matrix = new int[2, 2] { {2, 2}, {1, 1} };
    
    for (int i = 0; i < matrix.GetLength(0); i++)
    {
        for (int k = 0; k < matrix.GetLength(1); k++ )
        {
            //put a single value
            Console.Write(matrix[i,k]);
        }
        //next row
        Console.WriteLine();
    }
    
    0 讨论(0)
  • 2020-12-06 23:32

    Try like this..

            int[,] matrix = new int[3, 3]
            {
                {1, 2, 3},
                {4, 5, 6},
                {7, 8, 9},
            };
    
            int rowLength = matrix.GetLength(0);
            int colLength = matrix.GetLength(1);
    
            for (int i = 0; i < rowLength; i++)
            {
                for (int j = 0; j < colLength; j++)
                {
                    Console.Write(string.Format("{0} ", matrix[i, j]));
                }
                Console.Write(Environment.NewLine + Environment.NewLine);
            }
    
    
            Console.Read();
    
    0 讨论(0)
  • 2020-12-06 23:36

    You should read MSDN:Using foreach with Arrays

    int[,] numbers2D = new int[3, 2] { { 9, 99 }, { 3, 33 }, { 5, 55 } };
    // Or use the short form:
    // int[,] numbers2D = { { 9, 99 }, { 3, 33 }, { 5, 55 } };
    
    foreach (int i in numbers2D)
    {
        System.Console.Write("{0} ", i);
    }
    

    // Output: 9 99 3 33 5 55

    0 讨论(0)
提交回复
热议问题