30

我有一个二维数组,如下所示:

long[,] arr = new long[4, 4] {{ 0, 0, 0, 0 },
                              { 1, 1, 1, 1 },
                              { 0, 0, 0, 0 },
                              { 1, 1, 1, 1 }};

我想以矩阵格式打印该数组的值,例如:

0 0 0 0
1 1 1 1
0 0 0 0
1 1 1 1

我怎样才能做到这一点?

4

7 回答 7

52

您可以这样做(稍微修改数组以显示它适用于非方形数组):

        long[,] arr = new long[5, 4] { { 1, 2, 3, 4 }, { 1, 1, 1, 1 }, { 2, 2, 2, 2 }, { 3, 3, 3, 3 }, { 4, 4, 4, 4 } };

        int rowLength = arr.GetLength(0);
        int colLength = arr.GetLength(1);

        for (int i = 0; i < rowLength; i++)
        {
            for (int j = 0; j < colLength; j++)
            {
                Console.Write(string.Format("{0} ", arr[i, j]));
            }
            Console.Write(Environment.NewLine + Environment.NewLine);
        }
        Console.ReadLine();
于 2012-10-10T19:36:24.677 回答
9

像这样:

long[,] arr = new long[4, 4] { { 0, 0, 0, 0 }, { 1, 1, 1, 1 }, { 0, 0, 0, 0 }, { 1, 1, 1, 1 } };

var rowCount = arr.GetLength(0);
var colCount = arr.GetLength(1);
for (int row = 0; row < rowCount; row++)
{
    for (int col = 0; col < colCount; col++)               
        Console.Write(String.Format("{0}\t", arr[row,col]));
    Console.WriteLine();
} 
于 2012-10-10T19:27:36.960 回答
7

我写了扩展方法

public static string ToMatrixString<T>(this T[,] matrix, string delimiter = "\t")
{
    var s = new StringBuilder();

    for (var i = 0; i < matrix.GetLength(0); i++)
    {
        for (var j = 0; j < matrix.GetLength(1); j++)
        {
            s.Append(matrix[i, j]).Append(delimiter);
        }

        s.AppendLine();
    }

    return s.ToString();
}

要使用只需调用该方法

results.ToMatrixString();
于 2019-11-15T15:37:13.080 回答
2

以下是在Unity中的操作方法:

(来自@markmuetz 的修改答案,所以一定要支持他的答案

int[,] rawNodes = new int[,]
{
    { 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 0 }
};

private void Start()
{
    int rowLength = rawNodes.GetLength(0);
    int colLength = rawNodes.GetLength(1);
    string arrayString = "";
    for (int i = 0; i < rowLength; i++)
    {
        for (int j = 0; j < colLength; j++)
        {
            arrayString += string.Format("{0} ", rawNodes[i, j]);
        }
        arrayString += System.Environment.NewLine + System.Environment.NewLine;
    }

    Debug.Log(arrayString);
}
于 2018-03-04T01:06:17.627 回答
0

您可以在短时间内做到这一点。

        int[,] values=new int[2,3]{{2,4,5},{4,5,2}};

        for (int i = 0; i < values.GetLength(0); i++)
        {
            for (int k = 0; k < values.GetLength(1); k++) {
                Console.Write(values[i,k]);
            }

            Console.WriteLine();
        }
于 2013-03-30T09:05:53.587 回答
0

你也可以这样做

        long[,] arr = new long[4, 4] { { 0, 0, 0, 0 }, { 1, 1, 1, 1 }, { 0, 0, 0, 0 }, { 1, 1, 1, 1 }};

        for (int i = 0; i < arr.GetLength(0); i++)
        {
            for (int j = 0; j < arr.GetLength(1); j++)
            {
                Console.Write(arr[i,j]+" ");
            }
            Console.WriteLine();
        }
于 2018-08-22T03:16:49.950 回答
0

如果使用方阵:

    int[,] mat = new int[,]{{ 1, 0, 0 },{ 0, 1, 0},{ 0, 0, 1}};
    int i=1;
    foreach(int e in mat){
        Console.Write(i%Math.Sqrt(mat.Length)==0? $"{e}\n" : e);
        i+=1;
    }
于 2021-11-16T20:20:49.400 回答