Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 614
iCoder4777 Avatar asked Oct 10 '12 19:10

iCoder4777


People also ask

Is a 2D array a matrix?

The two dimensional (2D) array in C programming is also known as matrix. A matrix can be represented as a table of rows and columns.

Can you print a 2D array in Java?

We know that a two-dimensional array in Java is a single-dimensional array having another single-dimensional array as its elements. We can use the Arrays. toString() method to print string representation of each single-dimensional array in the given two-dimensional array.


4 Answers

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 image 152
markmuetz Avatar answered Oct 04 '22 01:10

markmuetz


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();
} 
like image 25
DiverseAndRemote.com Avatar answered Oct 04 '22 00:10

DiverseAndRemote.com


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();
like image 43
Dimmduh Avatar answered Oct 04 '22 01:10

Dimmduh


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);
}
like image 28
Aggressor Avatar answered Oct 03 '22 23:10

Aggressor