# Java Array Tutorials
# Java Array Programs
➤ Find Length of Array
➤ Different ways to Print Array
➤ Sum of Array Elements
➤ Average of Array Elements
➤ Sum of Two Arrays Elements
➤ Compare Two Arrays in Java
➤ 2nd Largest Number in Array
➤ How to Sort an Array in Java
➤ Reverse an Array in Java
➤ GCD of N Numbers in Java
➤ Linear Search in Java
➤ Binary Search in Java
➤ Copy Array in Java
➤ Merge 2 Arrays in Java
➤ Merge two sorted Arrays
➤ Largest Number in Array
➤ Smallest Number in Array
➤ Remove Duplicates
➤ Insert at Specific Position
➤ Add Element to Array
➤ Remove Element From Array
➤ Count Repeated Elements
➤ More Array Programs
Java Matrix Programs
➤ Matrix Tutorial in Java
➤ Print 2D Array in Java
➤ Print a 3×3 Matrix
➤ Sum of Matrix Elements
➤ Sum of Diagonal Elements
➤ Row Sum – Column Sum
➤ Matrix Addition in Java
➤ Matrix Subtraction in Java
➤ Transpose of a Matrix in Java
➤ Matrix Multiplication in Java
➤ Menu-driven Matrix Operations
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, is 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 problems. 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:-
- Sum of matrix elements in Java
- Sum of Diagonal Elements of Matrix in Java
- Row sum and Column sum of Matrix in Java
- Matrix Addition in Java
- Subtraction of two matrices in Java
- Transpose of a Matrix in Java
- Matrix Multiplication in Java
- 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 do you find anything incorrect? Let us know in the comments. Thank you!