在Java编程中打印二维数组或矩阵
在本文中,我们将尝试以与通常在纸上书写相同的方式在控制台上打印数字数组或数字矩阵。
为此,逻辑是一个接一个地访问数组的每个元素,并使其以空格分隔打印,当行到达矩阵中的emd时,我们还将更改行
示例
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.
}
}
}输出结果
1 2 3 4 5 6 7 8 9
热门推荐
10 朋友新年祝福语大全 简短
11 新年祝福语简短大方兔年
12 搬新家礼物祝福语简短
13 同学见面花束祝福语简短
14 五一假期祝福语幽默简短
15 离职欢送敬酒祝福语简短
16 对学弟的祝福语简短
17 考老师辞职祝福语简短
18 祝福语驱散霉运的话简短