* Creating 2D Arrays

We looked at a few examples of methods that take 2D arrays as input and produce as 
a result 2D arrays that are generated based on the information in the input 2D arrays.

Keep in mind that when the size of the 2D array is known in advance we can create 
a table of the given number of elements:

int[][] myTable = new int[3][4];         // myTable has 3 rows of 4 columns of
                                         // empty cells (initialized to 0)
Example: Scale the Elements in a table
Example: Write a method scaleElements which takes a 2D array of and returns
a modified copy such that each original value is multiplied by a given factor.
retained as shown below; the rest of the elements are set to 0. 
The processing is similar to printTable: one row at a time, one column per row. The difference is that we do not print, but instead update the current cell. Here is the Java code:
    void scaleElements( double[][] table, double factor )
    {
        for (int r = 0 ; r < table.length ; r++ )
        {
            for (int c = 0 ; c < table[r] ; c++ )
            {
                table[r][c] =  table[r][c] * factor;
            }
        }
    }
This method will work even for tables that have rows with different lengths:
    double[][] data1 = { {1, 2, 3, 4},
                         {5, 6, 7, 8},
                         {9, 0, 1, 2} };

    scaleElements( data1, 4 );
    printTable( data1 );

    double[][] data2 = { {1, 2, 3, 4, 1, 2, 3, 4},
                         {5, 6},
                         {9, 0, 1, 2} };
                         {7} };

    scaleElements( data2, 4 );
    printTable( data2 );
Example: Write a version of scaleElements that returns new table This is similar to the previous version but it requires a result table. To simplify the task of creating the result table, we will assume that all rows have the same number of columns.
    void scaleElements( double[][] table, double factor )
    {
        int numRows = table.length;
        int numCols = table[0].length;

        double[][] result = new double[numRows][numCols];

        for ( int r = 0 ; r < numRows ; r++ )
        {
            for (int c = 0 ; c < numCols ; c++ )
            {
                result[r][c] =  table[r][c] * factor;
            }
        }

        return result;
    }
This method only works with tables that have the same number of columns:
    double[][] data = { {1, 2, 3, 4},
                        {5, 6, 7, 8},
                        {9, 0, 1, 2} };

    double[][] newData = scaleleElements( data, 4 );
    printTable( data );
Example: Write a version of scaleElements that returns new non-rectangular table This is similar to the previous version but the full final table cannot be created right awa6: 1. first, need to create table that has enough empty spots for the rows 2. then, create each new row at the time the current row is processed since length will be known
    void scaleElements( double[][] table, double factor )
    {
        int numRows = table.length;   // only rows known (the rows have different columns)

        double[][] result = new double[numRows][];  // created enough cells for rows { _, _, ..., _ } 

        for ( int r = 0 ; r < numRows ; r++ )
        {
            int numCols = table[r].length;          // get columns of current row
            result[r] = new double[ numCols ];      // fill _ above with new row of same size
                              
            for (int c = 0 ; c < numCols ; c++ )
            {
                result[r][c] =  table[r][c] * factor;
            }
        }

        return result;
    }
    double[][] data = { {1, 2, 3},       // 3 columns
                        {4, 5},          // 2 columns
                        {7, 8, 9, 0} };  // 4 columns

    double[][] newData = scaleleElements( data, 4 );
    printTable( data );