Intro to Multi-dimensional Arrays


Outline

  1. Multi-dimensional arrays.
      1. Basics of multi-dimensional arrays
        1. Row, column
        2. Length 
        3. Multi-dimensional arrays are arrays of arrays of ??

    Multi-dimensional arrays

    Examples:
    1. CorpSales.java - Demonstrates creating and filling a two dimensional array with primitive values and then totalling all elements.
    2. Lengths.java - Demonstrates the use of the length attribute and initializer lists with two dimensional arrays.
    3. MonthsDays.java - Demonstrates the array of array nature of multidimensional arrays.  The "rows" don't have to be of the same length.

    Ragged Array - An array of arrays in which the rows are of different sizes.  

    Exercise:

    Given the following method header:

    /********************************************************
     * getTotal returns the total value of all elements of the matrix
     * @param matrix a two dimensional matrix of sales numbers
     ********************************************************/
       public double getTotal(double [][] matrix)
      
    1. What are all of the possible states of this array of arrays? (Draw some pictures)
    2. What code would you write to calculate the total of all of the elements of this array?

    3. CS 239 - Spring 2012

      Department of Computer Science Nancy Harris Home Page Current Classes Link