/*
 *  =============================================================
 *  DemoArrays2.java: Demo features of two-dimensional arrays of
 *                    numbers and strings in Java
 *
 *  Note. A two-dimensional array is modeled as an array of one-
 *  dimensional arrays. This implies that the length of each 
 *  row need not be the same length.
 *
 *  Written By: Mark Austin                            April 2008
 *  =============================================================
 */

import java.text.*;

public class DemoArrays2 {

   // Constructor method.

   public DemoArrays2() {};

   // Setup a constant for the two-dimensional array size.

   public static final int ArraySize = 6;

   // main method : this is where the program execution begins.

   public static void main ( String [] args ) {

       // [a] Create and populate a two dimensional array of ten integers.
       //     The array has two rows and five columns.

       int [][] iaaA  = new int[2][5];
       for ( int i = 0; i <= 1; i = i + 1)
       for ( int j = 0; j <= 4; j = j + 1)
            iaaA [i][j] = i+j;

       // [b] Print contents of iA

       System.out.println("Array: iaaA");
       System.out.println("-----------");
       for ( int i = 0; i < 2; i = i + 1) {
           for ( int j = 0; j < 5; j = j + 1) 
              System.out.printf(" %4d ", iaaA [i][j] );
           System.out.printf("\n");
       }

       // [c] Retrieve and print the length of iA...

       System.out.println("");
       System.out.println("No of rows = " + iaaA.length );
       System.out.println("Length of row 1 = " + iaaA[0].length );
       System.out.println("Length of row 2 = " + iaaA[1].length );

       // [d] Print matrix using built-in length parameters ....

       System.out.println("Array: iaaA");
       System.out.println("-----------");
       for ( int i = 0; i < iaaA.length; i = i + 1) {
           for ( int j = 0; j < iaaA[i].length; j = j + 1) 
              System.out.printf(" %4d ", iaaA [i][j] );
           System.out.printf("\n");
       }

       // [e] Create and print (6x6) matrix of floating point numbers.

       double [][] daaA  = new double[ArraySize][ArraySize];
       for ( int i = 0; i < daaA.length; i = i + 1) 
       for ( int j = 0; j < daaA[i].length; j = j + 1) 
             daaA[i][j] = (double) (i + j);

       System.out.println("Array: daaA");
       System.out.println("-----------");
       for ( int i = 0; i < daaA.length; i = i + 1) {
           for ( int j = 0; j < daaA[i].length; j = j + 1) 
              System.out.printf(" %11.4e ", daaA [i][j] );
           System.out.printf("\n");
       }

       // [f] Create, initialize and print a two-dimensional array
       //     of strings....they are arrays-of-arrays.

       String skiResorts [][] = { {"Coronet Peak",      "New Zealand" },
                                      {"Heavenly",       "California" },
                                    {"Grouse Mtn",        "Vancouver" },
                                      {"Mt Baker",       "Washington" },
                                          {"Vail",         "Colorado" },
                            {"Whistler/Blackcomb", "British Columbia" } };

       System.out.println("");
       System.out.println("No of skiResorts = " + skiResorts.length );
       System.out.println("Length of first row  = " + skiResorts[0].length );
       System.out.println("Length of second row = " + skiResorts[1].length );

       System.out.println("");
       System.out.println("Array: skiResorts ");
       for ( int i = 0; i < skiResorts.length; i = i + 1)
            System.out.println( "  " + skiResorts [i][0] +
                                " is in " + skiResorts [i][1] );

       // ===================================================================
       // [g] Two-dimensional arrays need not have rows of equal length.
       //     There are two ways of handling the array setup.
       // ===================================================================

       // Method 1. Let the compiler figure out the number of rows and
       //           the length of each row.

       System.out.println("");
       System.out.println("Test ragged arrays with variable row length");
       System.out.println("-------------------------------------------");
       System.out.println("Method 1: Compiler determines details");

       int [][] iaaB = {{1,2},{3,4,5},{6,7,8,9},{10}};

       System.out.println("");
       System.out.println("No of rows      = " + iaaB.length );
       System.out.println("Length of row 1 = " + iaaB[0].length );
       System.out.println("Length of row 2 = " + iaaB[1].length );
       System.out.println("Length of row 3 = " + iaaB[2].length );
       System.out.println("Length of row 4 = " + iaaB[3].length );

       System.out.println("Array: iaaB");
       System.out.println("-----------");
       for(int i = 0; i < iaaB.length; i=i+1) {
           for(int j = 0; j < iaaB[i].length; j=j+1) {
               System.out.printf(" %3d ", iaaB[i][j] );
           }
           System.out.printf("\n" );
       }

       // Method 2. Explicit assembly of the array structure..

       System.out.println("");
       System.out.println("Method 2: Explicit assembly of the array structure");

       int[][] iaaC = new int[4][];    // Create number of rows...
       iaaC[0] = new int[2];           // Create memory for row 1.
       iaaC[1] = new int[3];           // Create memory for row 2.
       iaaC[2] = new int[4];           // Create memory for row 3.
       iaaC[3] = new int[1];           // Create memory for row 4.

       iaaC[0][0] =  1; iaaC[0][1] = 2;
       iaaC[1][0] =  3; iaaC[1][1] = 4; iaaC[1][2] = 5;
       iaaC[2][0] =  6; iaaC[2][1] = 7; iaaC[2][2] = 8; iaaC[2][3] = 9;
       iaaC[3][0] = 10;

       System.out.println("Array: iaaC");
       System.out.println("-----------");
       for(int i = 0; i < iaaC.length; i=i+1) {
           for(int j = 0; j < iaaC[i].length; j=j+1) {
               System.out.printf(" %3d ", iaaC[i][j] );
           }
           System.out.printf("\n" );
       }
   }
}
