【发布时间】:2019-11-19 21:22:17
【问题描述】:
我正在尝试打印二维数组 (a) 的“中间”。例如,对于我的代码中的给定数组,我想打印:
[3,4,5,6]
[4,5,6,7]
但是我只能打印出“中间”值。我想根据说明修改二维数组(n),然后打印出来。 我该怎么做呢?
这是我的代码:
public static int[][] inner (int[][] a) {
int rowL = a.length - 1;
int colL = a[1].length -1;
for (int row = 1; row < rowL; row++) {
for (int col = 1; col < colL ; col++) {
System.out.print(a[row][col]);
}
System.out.println();
}
return a;
}
public static void main(String[] args) {
int [][] a = { {1,2,3,4,5,6},
{2,3,4,5,6,7},
{3,4,5,6,7,8},
{4,5,6,7,8,9} };
for (int[] row : a) {
System.out.println(Arrays.toString(row));
}
System.out.println();
for ( int[] row : inner(a) ) {
System.out.println(Arrays.toString(row));
}
}
【问题讨论】:
标签: java arrays for-loop multidimensional-array