package link; public class RingSetArray { int[][] array; private int startValue = 0; private int row = 0; private int col = 0; public RingSetArray(int row, int col) { this.row = row; this.col = col; array = new int[this.row][this.col]; setArrayValue(this.row, this.col, 0); } @Override public String toString() { StringBuffer buff = new StringBuffer(); for (int i = 0; i < this.row; i++) { for (int j = 0; j < this.col; j++) buff.append(array[i][j] + "\t"); buff.append("\n"); } return buff.substring(0, buff.length()-2); } private void setArrayValue(int row, int col, int start) { if (row >= 1 && col >= 2) { //上边界 System.out.print("上:"); for (int i = start; i < col; i++) { // 数组的索引为 [start][i] array[start][i] = startValue++; System.out.print(start + "" + i + " "); } //右边界 System.out.print("右:"); for (int i = start + 1; i < row; i++) { // 数组的索引为 [i][col-1] array[i][col-1] = startValue++; System.out.print(i + "" + (col-1) + " "); } //下边界 System.out.print("下:"); if(row > 1) for (int i = (col - 2); i >= start; i--) { // 数组的索引为 [row-1][i] array[row - 1][i] = startValue++; System.out.print((row - 1) + "" + i + " "); } //左边界 System.out.print("左:"); for (int i = (row - 2); i > start; i--) { // 数组的索引为 [i][start] array[i][start] = startValue++; System.out.print(i + "" + start + " "); } //row - 1, col - 1, start + 1 // 递归调用本函数 System.out.println(); setArrayValue(row - 1, col - 1, start + 1); } } }