English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
In this article, we will try to print a numeric array or numeric matrix on the console in the same way as it is usually written on paper.
To do this, the logic is to access each element of the array one by one, and print it with spaces, and we will also change the line when the row reaches the end of the matrix.
public class Print2DArray { public static void main(String[] args) { final int[][] matrix = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }; for (int i = 0; i < matrix.length; i++) { //this equals to the row in our matrix. for (int j = 0; j < matrix[i].length; j++) { //this equals to the column in each row. System.out.print(matrix[i][j] + " "); } System.out.println(); //change line on console as row comes to end in the matrix. } } }
Output Result
1 2 3 4 5 6 7 8 9