二维数组(N*N),沿对角线方向,从右上角打印到左下角如N=4:
4*4二维数组
- { 1 2 3 4 }
- { 5 6 7 8 }
- { 9 10 11 12 }
- {13 14 15 16 }
打印顺序
- 4
- 3 8
- 2 7 12
- 1 6 11 16
- 5 10 15
- 9 14
- 13
public class Test {
public static void main(String[] args){
int[][] DATA = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}};
int n = 4;
int iStart = 0;
int jStart = n - 1;
while (iStart != n) {
for (int i = iStart, j = jStart; i <= n - 1 && j <= n - 1; i++, j++) {
System.out.print(DATA[i][j]);
System.out.print(" ");
}
if (jStart > 0) {
jStart--;
} else {
iStart++;
}
System.out.println();
}
}
}