今日頭條--二維數組矩形輸出
阿新 • • 發佈:2018-08-22
public 數組 main || str == stat turn int
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 < number; k++) {
if (j > column - 1 - k) {
return;
}
for (j = k; j <= column - 1 - k; j++) {
System.out.printf(arr[i][j] + " ");
}
j--;
i = k + 1;
if (i > row - k - 1) {
return;
}
for (; i <= row - k - 1; i++) {
System.out.printf(arr[i][j] + " ");
}
i--;
j = column - k - 2;
if (j < k) {
return;
}
for (; j >= k; j--) {
System.out.printf(arr[i][j] + " ");
}
j++;
i = row - 2 - k;
if (i < k + 1) {
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);
}
}
今日頭條--二維數組矩形輸出