Skip to content
Related Articles

Related Articles

Improve Article

Print a 2 D Array or Matrix in Java

  • Difficulty Level : Easy
  • Last Updated : 19 Feb, 2021

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

Method 1 (Simple Traversal) 

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.

Java




// Java program to print the elements of
// a 2 D array or matrix
import java.io.*;
class GFG {
 
    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 

Method 2 (Using for-each loop



This is similar to the above. Instead of simple for loops, we use for each loop here. 

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 

Method 3 (Prints in matrix style Using Arrays.toString()) 

Arrays.toString(row) converts the complete row is converted as a string and 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.*;
class GFG {
 
    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]

Method 4 (Prints in matrix style Using Arrays.deepToString())

Arrays.deepToString(int[][]) converts  2D array to 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 GFG {
 
    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]]

This article is contributed by Nikita Tiwari & Lovesh Dongre. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :