public int[][] generateMatrix(int n) {
        int[][] result = new int[n][n];
        int up = 0;
        int down = n - 1;
        int left = 0;
        int right = n - 1;
        int index = 1;
        while (up <= down && left <= right) {
            for (int i = left; i <= right; i++) {
                result[up][i] = index++;
            }
            up++;

            for (int i = up; i <= down; i++) {
                result[i][right] = index++;
            }
            right--;

            for (int i = right; i >= left; i--) {
                result[down][i] = index++;
            }
            down--;

            for (int i = down; i >= up; i--) {
                result[i][left] = index++;
            }
            left++;
        }
        return result;
    }
Date 07/31/2016
@author Tushar Roy

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.

https://leetcode.com/problems/spiral-matrix-ii/
src/com/interview/multiarray/SpiralGeneration.java