打印数组:

1  3  4  10  11

2  5  9  12

6  8  13

7  14

15

 

package com.raystorm.test;

public class PrintArrayByOddEvenNumber {
    static void printArray (int height) {
        int i = 0, j = 0, value = 0;
        final int row = 10, column = 10;
        int times = 1;
        int [][] array = new int [row][column];
        for (int k = 0; k < height; k ++) {
            //construct array
            if (k % 2 == 0) {
                i = 0;
                while (i <= k) {
                    j = k - i;
                    array[i][j] = ++value;
                    i ++;
                }
            } else {
                j = 0;
                while (j <= k) {
                    i = k - j;
                    array[i][j] = ++value;
                    j ++;
                }
            }
            //auto expand volume of array
            if (k >= row * times / 2) {
                int[][] tempArray = array;
                times++;
                array = new int[row * times][column * times];
                for (int tempOut = 0; tempOut < row * (times - 1); tempOut++) {
                    for (int tempIn = 0; tempIn < column * (times - 1); tempIn++) {
                        array[tempOut][tempIn] = tempArray[tempOut][tempIn];
                    }
                }
            }
        }
        //print all array
        for (int x = 0; x < height; x ++) {
            for (int y = 0; y < height; y ++) {
                if (array[x][y] == 0) continue;
                System.out.print(array[x][y] + " ");
            }
            System.out.println();
        }
    }
    
    public static void main(String[] args) {
        printArray(16);
    }
}