简体   繁体   English

以矩阵格式打印二维数组

[英]Printing 2D array in matrix format

I have a 2D array as follows:我有一个二维数组,如下所示:

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

I want to print the values of this array in matrix format like:我想以矩阵格式打印此数组的值,例如:

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

How can I do this?我怎样才能做到这一点?

You can do it like this (with a slightly modified array to show it works for non-square arrays):你可以这样做(稍微修改数组以显示它适用于非方形数组):

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

like so:像这样:

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

I wrote extension method我写了扩展方法

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

To use just call the method使用只需调用该方法

results.ToMatrixString();

Here is how to do it in Unity :以下是在Unity 中的操作方法

(Modified answer from @markmuetz so be sure to upvote his answer ) (来自@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);
}

Your can do it like this in short hands.你可以在短期内做到这一点。

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

you can do like this also你也可以这样做

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

If using a square matrix:如果使用方阵:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM