Spiral.java


Below is the syntax highlighted version of Spiral.java from §1.4 Arrays.


/******************************************************************************
 *  Compilation:  javac Spiral.java
 *  Execution:    java Spiral n
 *
 *  Print spiral in n-by-n grid.
 * 
 *  % java Spiral 4
 *  1
 *  2
 *  3
 *  4
 *  8
 *  12
 *  16
 *  15
 *  14
 *  13
 *  9
 *  5
 *  6
 *  7
 *  11
 *  10
 *
 ******************************************************************************/


public class Spiral {
    public static void main(String[] args) { 
        int n = Integer.parseInt(args[0]);

        // create n-by-n array of integers 1 through n
        int[][] a = new int[n][n];
        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                a[i][j] = 1 + n*i + j;

        // print n-by-n array of integers 1 through n
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                System.out.printf("%2d ", a[i][j]);
            }
            System.out.println();
        }
        System.out.println();

        // spiral
        for (int i = n-1, j = 0; i > 0; i--, j++) {
            for (int k = j; k < i; k++)
                System.out.println(a[j][k]);
            for (int k = j; k < i; k++)
                System.out.println(a[k][i]);
            for (int k = i; k > j; k--)
                System.out.println(a[i][k]);
            for (int k = i; k > j; k--)
                System.out.println(a[k][j]);
        }
     
        // special case for middle element if n is odd
        if (n % 2 != 0)
            System.out.println(a[(n-1)/2][(n-1)/2]);
    }

}


Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne.
Last updated: Fri Oct 20 14:12:12 EDT 2017.