JMU
Multi-Dimensional Arrays
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Review of Arrays
Review of Arrays (cont.)
Review of Arrays (cont.)
images/array.gif
Review of Arrays (cont.)
Review of Arrays (cont.)
Motivation for Arrays of Arrays
Opinions?
    // Keep track of the points scored in each game for week 0
    int[] pointsScored0 = {58, 60};
    
    // Keep track of the points scored in each game for week 1
    int[] pointsScored1 = {72, 48, 61, 44};
    
    // Keep track of the points scored in each game for week 2
    int[] pointsScored2 = {60, 63, 51};
    
    
Motivation for Arrays of Arrays (cont.)
Arrays of Arrays
Arrays of Arrays (cont.)

An Example

javaexamples/multidimensionalarrays/Sports1.java (Fragment: 0)
        int          game, points, week;
        int[]        temp;
        Object[]     pointsScored;


        pointsScored    = new Object[3];

        temp            = new int[2];
        temp[0]         = 58;
        temp[1]         = 60;
        pointsScored[0] = temp;

        temp            = new int[4];
        temp[0]         = 72;
        temp[1]         = 48;
        temp[2]         = 61;
        temp[3]         = 44;
        pointsScored[1] = temp;

        temp            = new int[3];
        temp[0]         = 60;
        temp[1]         = 63;
        temp[2]         = 51;
        pointsScored[2] = temp;


        for (week=0; week < pointsScored.length; week++)
        {
            System.out.printf("\nWeek: %s\n", week);

            temp = (int[])pointsScored[week];

            for (game=0; game < temp.length; game++)
            {
                points = temp[game];
                System.out.printf("  Game %d: %d\n", game, points);
            }
        }
        
Arrays of Arrays (cont.)
Arrays of Arrays (cont.)
Arrays of Arrays (cont.)

An Example

javaexamples/multidimensionalarrays/Sports2.java (Fragment: 0)
        int          game, points, week;
        int[][]      pointsScored;        // Note the declaration


        // Create a "group" of arrays.  There are
        // 3 arrays in the "group".
        pointsScored    = new int[3][];

        // Create array zero.  There are 2 elements
        // in the array.
        pointsScored[0]    = new int[2];
        pointsScored[0][0] = 58;          // Note the use of [][]
        pointsScored[0][1] = 60;          // Note the use of [][]

        // Create array one.  There are 4 elements
        // in the array.
        pointsScored[1]    = new int[4];
        pointsScored[1][0] = 72;
        pointsScored[1][1] = 48;
        pointsScored[1][2] = 61;
        pointsScored[1][3] = 44;


        // Create array two.  There are 3 elements
        // in the array.
        pointsScored[2]    = new int[3];
        pointsScored[2][0] = 60;
        pointsScored[2][1] = 63;
        pointsScored[2][2] = 51;


        // Note the use of pointsScored.length
        for (week=0; week < pointsScored.length; week++)
        {
            System.out.printf("\nWeek: %s\n", week);

            // Note the use of pointsScored[].length
            // (Watch for ArrayIndexOutOfBounds problems)
            for (game=0; game < pointsScored[week].length; game++)
            {
                System.out.printf("  Game %d: %d\n", game,
                                                     pointsScored[week][game]);
            }
        }
        
Arrays of Arrays (cont.)
Rectangular Arrays of Arrays
Rectangular Arrays of Arrays (cont.)

An Example

javaexamples/multidimensionalarrays/HumanResources1.java (Fragment: 0)
        int          column, employees, quarters, row;
        int[][]      sickDays;            // Note the declaration

        employees = 2;
        quarters  = 4;

        sickDays = new int[employees][quarters];

        // Employee 0
        sickDays[0][0] = 5; // 5 sick days in quarter 0
        sickDays[0][1] = 0; // 0 sick days in quarter 1
        sickDays[0][2] = 1; // 1 sick days in quarter 2
        sickDays[0][3] = 0; // 0 sick days in quarter 3

        // Employee 1
        sickDays[1][0] = 2; // 2 sick days in quarter 0
        sickDays[1][1] = 1; // 1 sick days in quarter 1
        sickDays[1][2] = 4; // 4 sick days in quarter 2
        sickDays[1][3] = 3; // 3 sick days in quarter 3

        for (row=0; row < employees; row++)
        {
            for (column=0; column < quarters; column++)
            {
                System.out.printf("%d\t", sickDays[row][column]);
            }
            System.out.printf("\n");
        }
        
Rectangular Arrays of Arrays (cont.)
Rectangular Arrays of Arrays (cont.)

Initializing with a "Literal"

javaexamples/multidimensionalarrays/HumanResources2.java (Fragment: 0)
        int          column, employees, quarters, row;

        // Note the literal
        int[][]      sickDays = {
                                 {5, 0, 1, 0},
                                 {2, 1, 4, 3},
                                };

        employees = sickDays.length;
        quarters  = sickDays[0].length;


        for (row=0; row < employees; row++)
        {
            for (column=0; column < quarters; column++)
            {
                System.out.printf("%d\t", sickDays[row][column]);
            }
            System.out.printf("\n");
        }
        
Rectangular Arrays of Arrays (cont.)

Generating all of the Elements

javaexamples/multidimensionalarrays/TimesTable.java (Fragment: 0)
        int      column, row, size;
        int[][]  table;

        size  = 10;
        table = new int[size][size];

        // Generate the entries
        for (row=0; row < size; row++)
        {
            for (column=0; column < size; column++)
            {
                table[row][column] = row * column;
            }
        }
        
Rectangular Arrays of Arrays (cont.)
Rectangular Arrays of Arrays (cont.)

Iteration (e.g., for Output)

javaexamples/multidimensionalarrays/TimesTable.java (Fragment: 1)
        // Print the entries
        for (row=0; row < size; row++)
        {
            for (column=0; column < size; column++)
            {
                System.out.printf("%d\t", table[row][column]);
            }
            System.out.printf("\n");
        }
        
General/Irregular Arrays of Arrays

Iteration (e.g., for Output)

javaexamples/multidimensionalarrays/TableUtilities.java (Fragment: 2)
    /**
     * Print an array of arrays of String objects on System.out.
     *
     * @param data   The data
     * @param width  The width of each column
     */
    public static void print(String[][] data, int width)
    {
        for (int r = 0; r < data.length; r++)        // What is data.length ?
        {
            for (int c = 0; c < data[r].length; c++) // What is data[r].length?
            {
                System.out.printf("%"+width+"s", data[r][c]);
            }
            System.out.printf("\n");
        }
    }