【发布时间】:2019-11-20 09:24:29
【问题描述】:
我正在尝试打印二维数组 (a) 的“中间”。例如,对于我的代码中的给定数组,我想打印:
[3,4,5,6]
[4,5,6,7]
但是我只能打印出“中间”值。我想在方法 inner 中修改 2D 数组 (a) 并将其打印在 main 中,而不是在嵌套的 for 循环中使用 System.out.println。我该怎么做呢?
这是我的代码:
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]);
a = new int[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 multidimensional-array