如何在C#中将2D数组打印到控制台

时间:2014-06-07 05:40:27

标签: c# console-application multidimensional-array

我没有任何代码,但我确实想知道如何做到这一点。如果重要的话,我会使用visual studio 2010 C#。

由于

杰森

10 个答案:

答案 0 :(得分:10)

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

答案 1 :(得分:3)

您应该阅读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);
}

//输出:9 99 3 33 5 55

答案 2 :(得分:3)

你可以在一行打印

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

输出

1 2 3 4 5 6 7 8

答案 3 :(得分:0)

这样做:

static public void Print2DArray(int[][] A)
{
    foreach (int[] row in A)
    {
        foreach (int element in row)
        {
              Console.Write(element.ToString() + " ");
        }
        Console.WriteLine();
    }
}

答案 4 :(得分:0)

以下是一个例子......

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

答案 5 :(得分:0)

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

答案 6 :(得分:0)

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;
    }

答案 7 :(得分:0)

试试这样..

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

答案 8 :(得分:0)

12.00

答案 9 :(得分:0)

以下是如何实现多维数组的多种解决方案,我认为在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);
            }
        }

    }
}