今日头条--二维数组矩形输出

2021-07-20 23:07

阅读:789

public class PrintArrayLoop {

public static void printArrayForLoopWay(int[][] arr) {
if (arr == null || arr.length == 0 || arr[0] == null || arr[0].length == 0) {
return;
}
int row = arr.length;
int column = arr[0].length;
int number = row > column ? (column - 1) : (row - 1);
int i = 0, j = 0;
for (int k = 0; k if (j > column - 1 - k) {
return;
}
for (j = k; j System.out.printf(arr[i][j] + " ");
}
j--;
i = k + 1;
if (i > row - k - 1) {
return;
}
for (; i System.out.printf(arr[i][j] + " ");
}
i--;
j = column - k - 2;
if (j return;
}
for (; j >= k; j--) {
System.out.printf(arr[i][j] + " ");
}
j++;
i = row - 2 - k;
if (i return;
}
for (; i >= k + 1; i--) {
System.out.printf(arr[i][j] + " ");
}
i++;
j++;
}
}

public static void main(String [] args){
int[][] arr=
{{1,2,3,4,5,6},{11,12,13,14,15,16},{21,22,23,24,25,26},{31,32,33,34,35,36},{41,42,43,44,45,46},{51,52,53,54,55,56}};
printArrayForLoopWay(arr);
}
}

上一篇:springboot配置路径

下一篇:C++ STL


评论


亲,登录后才可以留言!