# 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
Print Matrix or 2D array in Java | To print a matrix or 2D array or two-dimensional array, 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 Matrix or 2D Array using for loop
public class DisplayMatrix {
public static void main(String[] args) {
// declare and initialize a matrix or 2D array
int matrix[][] = { { 1, 2 }, { 4, 5 }, { 7, 8 } };
// 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
4 5
7 8
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.
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 a two-dimensional array or matrix using 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 2D array or 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
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]]
In this program, first, we had declared a 3×3 matrix, then we created the Scanner class object to read input values from the end-user. To read input value you can also use the BufferedReader class. The nested for loop is used to read those values. Finally, deepToString() method of the Arrays class is used to display the matrix. Arrays class contains overridden toString() method to display one-dimensional array and deepToString() method to display multi-dimensional array.
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!