A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/print-2-d-array-matrix-java/ below:

Print a 2D Array or Matrix in Java

Print a 2D Array or Matrix in Java

Last Updated : 24 Mar, 2025

In this article, we will learn to Print 2 Dimensional Matrix. 2D-Matrix or Array is a combination of Multiple 1 Dimensional Arrays. In this article we cover different methods to print 2D Array. When we print each element of the 2D array we have to iterate each element so the minimum time complexity is O( N *M ) where N is the number of rows in the matrix and M is the number of columns in the matrix.

Prerequisites: Arrays in Java, Array Declarations in Java (Single and Multidimensional)

Java Program to Print the 2D Array

We can find the number of rows in a matrix mat[][] using mat.length. To find the number of columns in i'th row, we use mat[i].length.

Example 1: Print a 2-dimensional array using nested for-loop.

Java
// Java program to print the elements of
// a 2 D array or matrix
import java.io.*;
class Geeks
{
    public static void print2D(int mat[][])
    {
        // Loop through all rows
        for (int i = 0; i < mat.length; i++)

            // Loop through all elements of current row
            for (int j = 0; j < mat[i].length; j++)
                System.out.print(mat[i][j] + " ");
    }

    public static void main(String args[])
        throws IOException
    {
        int mat[][] = { { 1, 2, 3, 4 },
                        { 5, 6, 7, 8 },
                        { 9, 10, 11, 12 } };
        print2D(mat);
    }
}

Output
1 2 3 4 5 6 7 8 9 10 11 12 

Complexity of the above method:

Other Methods to Print 2 Dimensional Array 1. Using for-each loop 

Example 2: Printing the 2D Array using the nested for-each loop

Java
// Java program to print the elements of
// a 2 D array or matrix using for-each
import java.io.*;
class GFG {

    public static void print2D(int mat[][])
    {
        // Loop through all rows
        for (int[] row : mat)

            // Loop through all columns of current row
            for (int x : row)
                System.out.print(x + " ");
    }

    public static void main(String args[])
        throws IOException
    {
        int mat[][] = { { 1, 2, 3, 4 },
                        { 5, 6, 7, 8 },
                        { 9, 10, 11, 12 } };
        print2D(mat);
    }
}

Output
1 2 3 4 5 6 7 8 9 10 11 12 

Complexity of the above method:

2. Using Arrays.toString() - Prints in matrix style

Example 3: Converts row into a string using Arrays.toString(row) then each row is printed in a separate line. 

Java
// Java program to print the elements of
// a 2 D array or matrix using toString()
import java.io.*;
import java.util.*;

// Driver Class
class Geeks 
{
    public static void print2D(int mat[][])
    {
        // Loop through all rows
        for (int[] row : mat)

            // converting each row as string
            // and then printing in a separate line
            System.out.println(Arrays.toString(row));
    }

    public static void main(String args[])
        throws IOException
    {
        int mat[][] = { { 1, 2, 3, 4 },
                        { 5, 6, 7, 8 },
                        { 9, 10, 11, 12 } };
        print2D(mat);
    }
}

Output
[1, 2, 3, 4]
[5, 6, 7, 8]
[9, 10, 11, 12]

Complexity of the above method:

3. Using Arrays.deepToString()

Example 4: Using Arrays.deepToString(int[][]) converts the 2D array to a string in a single step.

Java
// Java program to print the elements of
// a 2 D array or matrix using deepToString()
import java.io.*;
import java.util.*;

class Geeks 
{
    public static void print2D(int mat[][])
    {
        System.out.println(Arrays.deepToString(mat));
    }

    public static void main(String args[]) throws IOException
    {
        int mat[][] = { { 1, 2, 3, 4 },
                        { 5, 6, 7, 8 },
                        { 9, 10, 11, 12 } };
                        
        print2D(mat);
    }
}

Output
[[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]

Complexity of the above method:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4