public class 二维数组回形输出 {
public static void main(String[] args) {
int[][] p = new int[4][5];
for (int i = 0; i < p.length; i++) {
for (int j = 0; j < p[i].length; j++) {
int k = new Random().nextInt(1000);
p[i][j] = k;
System.out.print(k + " ");
}
System.out.println();
}
System.out.println();
int row = p.length, col = p[0].length;
int max = col * row;//输出结束则停止输出
int times = 0;
for (int n = 0; ; n++) {
for (int j = n; j < col - n; j++) {//输出回形的上面的行
System.out.println(p[n][j]);
times++;
}
if (max == times) break;
for (int i = n + 1; i < row - n; i++) {//输出回形的右边的列
System.out.println(p[i][col - n - 1]);
times++;
}
for (int j = col - n - 2; j >= n; j--) {
System.out.println(p[row - n - 1][j]);//输出回形的下面的行
times++;
}
if (max == times) break;
for (int i = row - n - 2; i > n; i--) {//输出回形的左边的列
System.out.println(p[i][n]);
times++;
}
if (max == times) break;
}
}
}
来源:CSDN
作者:蔡燃
链接:https://blog.csdn.net/qq_35758320/article/details/104029708