How to print 2D array to console in C#

后端 未结 10 1594
野性不改
野性不改 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:13
    private int[,] MirrorH(int[,] matrix)
        {
            int[,] MirrorHorizintal = new int[4, 4];
            for (int i = 0; i < 4; i++)
            {
                for (int j = 0; j < 4; j ++)
                {
                    MirrorHorizintal[i, j] = matrix[i, 3 - j];
                }
            }
            return MirrorHorizintal;
        }
    
    0 讨论(0)
  • 2020-12-06 23:14
        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();
            }
        }
    
    0 讨论(0)
  • 2020-12-06 23:16

    The following is an example...

    static void Main()
    {
        // A. 2D array of strings.
        string[,] a = new string[,]
        {
            {"ant", "aunt"},
            {"Sam", "Samantha"},
            {"clozapine", "quetiapine"},
            {"flomax", "volmax"},
            {"toradol", "tramadol"}
        };
    
        // B. Get the upper bound to loop.
        for (int i = 0; i <= a.GetUpperBound(0); i++)
        {
            string s1 = a[i, 0]; // ant, Sam, clozapine...
            string s2 = a[i, 1]; // aunt, Samantha, quetiapine...
            Console.WriteLine("{0}, {1}", s1, s2);
        }
    
        Console.WriteLine();
    }
    
    0 讨论(0)
  • 2020-12-06 23:17

    Do it like this:

    static public void Print2DArray(int[][] A)
    {
        foreach (int[] row in A)
        {
            foreach (int element in row)
            {
                  Console.Write(element.ToString() + " ");
            }
            Console.WriteLine();
        }
    }
    
    0 讨论(0)
  • 2020-12-06 23:21
    //The following are three ways to print any 2d arrays to console:
    int[,] twoDArray = new int[3, 4]{ {2,5,55,44},{10,45,5,22 },{ 67,34,56,77} };
                        Console.WriteLine("Array Code Out Method:1");
                        int rows = twoDArray.GetLength(0); // 0 is first dimension, 1 is 2nd 
                                                           //dimension of 2d array 
                        int cols = twoDArray.GetLength(1);
                        for (int i = 0; i < rows; i++)
                        {
                            for (int j = 0; j < cols; j++)
                            {
                                Console.Write("\t" + twoDArray[i, j]);
                                //output: 2       5       55      44      10      45      5       22      67      34      56      77
                            }
                        }
    
                        Console.WriteLine("Array Code Out Method: 2");
                        for (int x = 0; x <= twoDArray.GetUpperBound(0); x++)
                        {
                            for (int y = 0; y <= twoDArray.GetUpperBound(1); y++)
                            {
                                Console.Write("\t"+ twoDArray[x,y]);
                                //output: 2       5       55      44      10      45      5       22      67      34      56      77
                            }
                        }
    
                        Console.WriteLine("Array Code Out Method:3");
                        foreach (int items in twoDArray)
                        {
                            Console.Write(""+ "\t" +items);
                            //output:  2       5       55      44      10      45      5       22      67      34      56      77
                        }
    
                        //string example
                        string[,] friendNames = new string[,] { {"Rosy","Amy"},
                                                          {"Peter","Albert"}
                                                        };
                        foreach (string str in friendNames)
                        {
                            Console.WriteLine(str);
                        }
    
    0 讨论(0)
  • 2020-12-06 23:22

    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
    
    0 讨论(0)
提交回复
热议问题