- Forward


Multi-Dimensional Arrays
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review of Arrays
Back SMYC Forward
  • Some Awkward Code:
    • x1 = 550.75; x2 = 25.60; x3 = 347.10; x4 = 120.45; x5 = 885.00; x6 = 10.99; x7 = 123.45;
  • How To Handle This in Math:
    • \(x_i\)
  • How To Handle This in a Programming Language:
    • Use an array (e.g., named x) and refer to particular elements using the square bracket operator (e.g., x[i])
Review of Arrays (cont.)
Back SMYC Forward
  • Fixed and Known Number of Elements:
    • In order to be able to allocate a contiguous block of memory
  • Homogeneous Elements:
    • In order to know how big each element/offset is
Review of Arrays (cont.)
Back SMYC Forward
array
Review of Arrays (cont.)
Back SMYC Forward
  • Memory Allocation:
    • The new operator returns a memory address (reference) which is normally assigned to a variable
  • Starting Index:
    • The first element has an index of 0 since this is an offset from the first memory address
Review of Arrays (cont.)
Back SMYC Forward
  • Arrays of Primitive Types:
    • Each of the elements is a primitive of the same type
  • Arrays of Reference Types:
    • Each of the elements is a reference (and all references, regardless of what they refer to, are the same size)
Motivation for Arrays of Arrays
Back SMYC Forward
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.)
Back SMYC Forward
  • What We Have:
    • A bunch of individual arrays, each with its own identifier
  • What We Want:
    • An array of arrays (with one identifier)
Arrays of Arrays
Back SMYC Forward
  • Each Array:
    • Is a reference
  • The Array of Arrays:
    • We can achieve this with an array of Object objects (which is essentially an array of generic references)
Arrays of Arrays (cont.)
Back SMYC Forward

An Example

javaexamples/multidimensionalarrays/Sports1.java (Fragment: 0)
 
Arrays of Arrays (cont.)
Back SMYC Forward
  • A Quiz:
    • When is the name table used?
    • When is memory allocated from the heap?
  • A Question:
    • Can you illustrate both the name table and the heap for the previous example?
Arrays of Arrays (cont.)
Back SMYC Forward
  • An Inconvenience:
    • We have to cast each element of the Object array in order to use it
  • A Remedy:
    • Declare the type of each element explicitly
  • What This Leads To:
    • Multiple applications of the [] modifier in the declaration
    • Multiple applications of the [] operator when accessing
Arrays of Arrays (cont.)
Back SMYC Forward

An Example

javaexamples/multidimensionalarrays/Sports2.java (Fragment: 0)
 
Arrays of Arrays (cont.)
Back SMYC Forward
  • An Observation:
    • Each element of the array of arrays is a reference
  • An Implication:
    • It is possible to re-assign (i.e., replace) an entire element
  • A Question:
    • Can you illustrate both the name table and the heap for the previous example?
Rectangular Arrays of Arrays
Back SMYC Forward
  • A Special Case:
    • Each array has the same number of elements and that number is known
  • An Implication:
    • Both sizes are known when the memory is allocated
  • A Visualization:
    • rectangular-array
Rectangular Arrays of Arrays (cont.)
Back SMYC Forward

An Example

javaexamples/multidimensionalarrays/HumanResources1.java (Fragment: 0)
 
Rectangular Arrays of Arrays (cont.)
Back SMYC Forward
  • Terminology/Conventions:
    • Called a two-dimensional array
    • Normally, the first index refers to the row and the second refers to the column (which is the opposite of normal \((x,y)\) coordinates)
  • Common Applications:
    • Tables of data
    • Game boards (e.g., chess, checkers)
    • Images (i.e., 2-D array of pixels)
Rectangular Arrays of Arrays (cont.)
Back SMYC Forward

Initializing with a "Literal"

javaexamples/multidimensionalarrays/HumanResources2.java (Fragment: 0)
 
Rectangular Arrays of Arrays (cont.)
Back SMYC Forward

Generating all of the Elements

javaexamples/multidimensionalarrays/TimesTable.java (Fragment: 0)
 
Rectangular Arrays of Arrays (cont.)
Back SMYC Forward
  • Traversal Order:
    • Column-Major - Column 0 is traversed (downward), then column 1 is traversed (downward), ...
      column-major
    • Row-Major - Row 0 is traversed (rightward), then row 1 is traversed (rightward), ...
      row-major
  • Traversal Order and Nested Loops:
    • The outer loop corresponds to the major order
  • An Observation:
    • Since many output devices are row oriented, row-major order is often used for output (so the outer loop iterates over the rows)
Rectangular Arrays of Arrays (cont.)
Back SMYC Forward

Iteration (e.g., for Output)

javaexamples/multidimensionalarrays/TimesTable.java (Fragment: 1)
 
General/Irregular Arrays of Arrays
Back SMYC Forward

Iteration (e.g., for Output)

javaexamples/multidimensionalarrays/TableUtilities.java (Fragment: 2)
 
There's Always More to Learn
Back -