foreach循环输出二维数组

声明一个二维数组:

1
int[][] nums = new int[3][]; //可以只声明行数,不声明列数,反之不行;

初始化一个二维数组,并foreach循环遍历输出该数组的每一个元素:

1
2
3
4
5
6
7
int[][] num = {{78,98},{65,75,63},{98}};
for(int[] i:num) { //先定位到行
for(int j:i) { //再定位到列
System.out.print(j+" ");
}
System.out.println();
}

最后输出:

1
2
3
78 98 
65 75 63
98
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2021 Silver Shaded
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信