Printing 2D array in matrix format

后端 未结 6 1277
一整个雨季
一整个雨季 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:36

    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();
    
    0 讨论(0)
  • 2020-12-08 20:38

    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();
            }
    
    0 讨论(0)
  • 2020-12-08 20:43

    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();
    } 
    
    0 讨论(0)
  • 2020-12-08 20:50

    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();
            }
    
    0 讨论(0)
  • 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);
    }
    
    0 讨论(0)
  • 2020-12-08 20:53

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