// File: Arrays.java
// Copyright (C) 2010 Gregory D. Weber
// Examples of Java arrays

public class Arrays
{

    public static void main (String [] args)
    {
        // error:
        // double [] x;
        // System.out.println("x =");
        // showArray(x);

        double [] y = new double[5];
        System.out.println("y =");
        showArray(y);

        for (int i = 0; i < y.length; i++)
            y[i] = (float) i * 2 + 0.5;
        System.out.println("y =");
        showArray(y);

        double [] z = new double [] {2.5, 3.9, -4.7, 5.1};
        System.out.println("z =");
        showArray(z);

        // = new double [] [] is optional:
        double [] [] w = {{1.5, 3.0},
                          {3.5, 4.2, 9.9},
                          {0.0, 0.0, 0.0, 1.0},
                          {10.0, 9.9, 8.7, 7.5, 6.3}};
        System.out.println("w =");
        showArray2(w);
    }

    public static void showArray (double [] xs)
    {
        for (double x: xs)
            System.out.print(x + " ");
        System.out.println();
    }

    // Usually we'd write a nested loop to show a 2D array,
    // but we've already got something to show a 1D array,
    // and each row of a 2D is a 1D, so why not use that?
    public static void showArray2 (double [] [] xss)
    {
        for (double [] xs : xss)
            showArray(xs);
    }

}
