Printing 2D array in matrix format

后端 未结 6 1294
一整个雨季
一整个雨季 2020-12-08 20:12

I have a 2D array as follows:

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


        
6条回答
  •  轮回少年
    2020-12-08 20:51

    Here is how to do it in Unity:

    (Modified answer from @markmuetz so be sure to upvote his answer)

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

提交回复
热议问题