Java Program to Print 3×3 Matrix

Java Program to Print 3×3 Matrix | Print 3×3 Matrix using loops | Program to display 3×3 Matrix using Arrays.deepToString().

To print or display a 3×3 matrix we can use nested loops, it can be either for loop, for-each loop, while loop, or do-while loop. We have another better alternative deepToString() which is given in java.util.Arrays class. First, let us see the Java program using loops.

Java Program to Print 3×3 Matrix using for loop

public class DisplayMatrix {

  public static void main(String[] args) {
    // declare and initialize a 3x3 matrix
    int matrix[][] = 
      { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
    
    // display matrix using for loops
    // outer loop for row
    for(int i=0; i<matrix.length; i++) {
      // inner loop for column
      for(int j=0; j<matrix[0].length; j++) {
        System.out.print(matrix[i][j] + " ");
      }
      System.out.println(); // new line
    }
  }

}

Output:-

1 2 3
4 5 6
7 8 9

In this program matrix.length returns the row size and matrix[0].length returns the size of the column. See more:- How to find the length of the array in Java. Since we are assuming that the given matrix is of 3×3 matrix, therefore, no need to calculate the row and column size, directly place 3.

// display matrix using for loops
for(int i=0; i<3; i++) {
   for(int j=0; j<3; j++) {
      System.out.print(matrix[i][j] + " ");
   }
   System.out.println(); // new line
}

Print Using For-Each loop

The for-each or enhanced for loop was introduced in Java 1.5 version. Below is the Java program to display the 3×3 matrix using the for-each loop.

public class DisplayMatrix {

  public static void main(String[] args) {
    // declare and initialize a 3x3 matrix
    int matrix[][] = 
      { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
    
    // display 2D array using for-each loop
    for(int[] i : matrix) {
      for(int j : i) {
        System.out.print(j + " ");
      }
      System.out.println(); // new line
    }
  }

}

Output:-

1 2 3
4 5 6
7 8 9

Java Program to Print 3×3 Matrix using Arrays.deepToString() method

The java.util.Arrays class in Java contains several methods for basic array problem. It contains toString() method to display one-dimension array and deepToString() method to display the Java multi-dimensional array. Below program uses Arrays.deepToString() method to display the 3×3 matrix.

import java.util.Arrays;

public class DisplayMatrix {

  public static void main(String[] args) {
    // declare and initialize a 3x3 matrix
    int matrix[][] = 
      { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
    
    // display 2D array using for-each loop
    System.out.println(Arrays.deepToString(matrix)); 
  }

}

Output:-

[[1, 2, 3], [4, 5, 6], [7, 8, 9]]

Java Program to take 3×3 matrix elements as input from the user and display it

import java.util.Arrays;
import java.util.Scanner;

public class DisplayMatrix {

  public static void main(String[] args) {
    
    // declare a 3x3 matrix
    int matrix[][] = null ;
    matrix = new int[3][3];
    
    // create Scanner class object to read input
    Scanner scan = new Scanner(System.in);
    
    // read matrix
    System.out.println("Enter 3x3 Matrix elements: ");
    for(int i=0; i<3; i++) {
      for(int j=0; j<3; j++) {
        matrix[i][j] = scan.nextInt();
      }
    }
    
    // display 2D array using for-each loop
    System.out.println("Entered Matrix: ");
    System.out.println(Arrays.deepToString(matrix)); 
    
    // close Scanner 
    scan.close();
  }

}

Output:-

Enter 3×3 Matrix elements:
10 20 30
40 50 60
70 80 90
Entered Matrix:
[[10, 20, 30], [40, 50, 60], [70, 80, 90]]

See more matrix programs in Java:- 

  1. Sum of matrix elements in Java
  2. Sum of Diagonal Elements of Matrix in Java 
  3. Row sum and Column sum of Matrix in Java
  4. Matrix Addition in Java
  5. Subtraction of two matrices in Java 
  6. Transpose of a Matrix in Java 
  7. Matrix Multiplication in Java
  8. Menu-driven program for Matrix operations

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *