* 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: Copy Lower Left Triangle of Matrix
Example: Write a method copyLowerLeft which takes a 2D array of integers and returns
a copy of the original array such that only the elements below the diagonal are
retained as shown below; the rest of the elements are set to 0. 

(Assume that the input table is square, i.e. has same number of rows and columns.)

For example,

int[][] table = { { 1,  2,  3,  4},
                  { 5,  6,  7,  8},
                  { 9, 10, 11, 12},
                  {13, 14, 15, 16} };

int[] result = copyLowerLeft(table);
printTable(result);                      // displays   1  0  0  0
                                         //            5  6  0  0
                                         //            9 10 11  0
                                         //           13 14 15 16
The difference between this example and the previous examples is that the rows are traversed only partially -- for each row we need to traverse more and more columns. It is a good idea to list for each row the columns that need to be traversed in order to see a pattern that can help in the setup of the nested loops:
row 0, columns 0 .. 0
row 1, columns 1 .. 1
row 2, columns 1 .. 2
row 3, columns 1 .. 3
...
row r, columns 1 .. r

Here is the code outline:

1. Create a result 2D array that has as many rows as columns as the input array.
   (Keep in mind that all elements in the new array are automatically set to 0.)

2. For each row index in the input table:

       3. For each relevant column in the current row:

              4. Copy current element from input table
                 to corresponding entry in result table.

5. Return result table.
Here is the Java code:
    // returns a 2D array that contains the portion on or below the
    // main diagonal of the input 2D array; the input array is assumed
    // to have the same number of rows and columns
    int[] copyLowerLeft(int[][] table)
    {
        int size = table.length;
        int[][] result = new int[ size ][ size ];         // Step 1. (same rows and columns)

        for (int r = 0; r < table.length; r = r + 1)      // Step 2.
        {
            for (int c = 0; c <= r; c = c + 1)            // Step 2. (only columns 0 .. r)
            {
                result[r][c] =  table[r][c];              // Step 3.
            }
        }

        return result;                                    // Step 4.
    }
Example: Copy Upper Left Triangle of Matrix
Example: Write a method copyUpperLeft which takes a 2D array of integers and returns
a copy of the original array such that only the elements below the diagonal are
retained as shown below; the rest of the elements are set to 0. 

(Assume that the input table is square, i.e. has same number of rows and columns.)

For example,

int[][] table = { { 1,  2,  3,  4},
                  { 5,  6,  7,  8},
                  { 9, 10, 11, 12},
                  {13, 14, 15, 16} };

int[] result = copyUpperLeft(table);
printTable(result);                      // displays   1  2  3  4
                                         //            5  6  7  0
                                         //            9 10  0  0
                                         //           13  0  0  0
This is similar to the previous example. The only difference is in the pattern of columns that are traversed for each row. Once again it helps to list for each row the columns that need to be traversed in order to see a pattern that can help in the setup of the nested loops:
row 0, columns 0 .. all          (i.e. < length - 0)
row 1, columns 0 .. all but 1    (i.e. < length - 1)
row 2, columns 0 .. all but 2    (i.e. < length - 2)
row 3, columns 0 .. all but 3    (i.e. < length - 3)
...
row r, columns 0 .. all but r    (i.e. < length - r)

Here is the code outline:

1. Create a result 2D array that has as many rows as columns as the input array.
   (Keep in mind that all elements in the new array are automatically set to 0.)

2. For each row index in the input table:

       3. For each relevant column in the current row:

              4. Copy current element from input table
                 to corresponding entry in result table.

5. Return result table.
Here is the Java code:
    // returns a 2D array that contains the portion on or above the
    // main diagonal of the input 2D array; the input array is assumed
    // to have the same number of rows and columns
    int[] copyUpperLeft(int[][] table)
    {
        int size = table.length;
        int[][] result = new int[ size ][ size ];         // Step 1. (same rows and columns)

        for (int r = 0; r < table.length; r = r + 1)      // Step 2.
        {
            for (int c = 0; c < table.length - r; c = c + 1)            // Step 2. (all but last r columns)
            {
                result[r][c] =  table[r][c];              // Step 3.
            }
        }

        return result;                                    // Step 4.
    }
Example: Copy Upper Left Triangle of Matrix (traverse columns in reverse) Some may find it easier to think about the previous example in terms of traversing the columns in reverse. Again we create a list to see the exact pattern of where to start and stop:
row    column (start -> stop)
-----------------------------
0:     last -> 0              (i.e. length-1 -> 0)
1:     2nd last -> 0          (i.e. length-2 -> 0)
2:     3rd last -> 0          (i.e. length-3 -> 0)
3:     4th last -> 0          (i.e. length-4 -> 0)
...
r:     (r+1)th last -> 0      (i.e. length-(r+1) -> 0)
Here is the Java code:
    // returns a 2D array that contains the portion on or above the
    // main diagonal of the input 2D array; the input array is assumed
    // to have the same number of rows and columns
    int[] copyUpperLeft(int[][] table)
    {
        int size = table.length;
        int[][] result = new int[ size ][ size ];         // Step 1. (same rows and columns)

        for (int r = 0; r < table.length; r = r + 1)      // Step 2.
        {
            for (int c = table.length - (r+1); c >= 0; c = c - 1)            // Step 2. (start at last r-th column)
            {
                result[r][c] =  table[r][c];              // Step 3.
            }
        }

        return result;                                    // Step 4.
    }
Example: Copy Upper Left Triangle of Matrix (traverse by columns) In the examples so far we always traversed: - along the rows (outer loop) - then along the columns for the current row (inner loop) Sometimes it might be necessary (or convenient) to traverse: - along the columns (outer loop) - then along the rows (inner loop) In this example the traversal will be: - along the columns - bottom to top within a column For the above example we can sketch the patter column by column:
col    row [start -> stop]
-----------------------------
0:     last -> 0     (i.e. length-1 -> 0, all rows for first column)
1:     last-1 -> 0   (i.e. 1 above previous)
2:     last-2 -> 0   (i.e. 1 above previous, or 2 above last row)
3:     last-3 -> 0   (i.e. 1 above previous, or 3 above last row)
4:     last-4 -> 0   (i.e. 1 above previous, or 4 above last row)
...
c:     last-c -> 0   (i.e. c above last row, (table.length-1) - c)
                                              ^^^^^^^^^^^^^^  ^^^
                                                    last      c above
Here is the Java code:
    // returns a 2D array that contains the portion on or above the
    // main diagonal of the input 2D array; the input array is assumed
    // to have the same number of rows and columns
    int[] copyUpperLeft(int[][] table)
    {
        int size = table.length;
        int[][] result = new int[ size ][ size ];

        for (int c = 0; c < table.length; c = c + 1)
        {
            for (int r = (table.length - 1) - c ; r >= 0 ; r = r + 1)
            {
                result[r][c] =  table[r][c];
            }
        }

        return result;
    }