- Forward


Subarrays
A Programming Pattern


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Motivation
Back SMYC Forward
  • Recall:
    • A Java array has a length attribute so it is not necessary to store the length in another variable (as it is in some other languages)
  • An Implication:
    • In Java it is common to pass an array and operate on all of its elements
  • A Shortcoming:
    • We often want to operate only on some of the elements
Review
Back SMYC Forward
  • A Common Problem:
    • Write a method that is passed an array and must calculate the sum of the elements
  • A Solution You're Familiar With:
    • Use a for loop (because it's determinate)
    • Use the length attribute
Review (cont.)
Back SMYC Forward
javaexamples/programmingpatterns/subarrays/SubarraysMotivation.java (Fragment: 0)
 
Review (cont.)
Back SMYC Forward
  • A Shortcoming of this Solution:
    • It does not allow you to find the sum of a subset of the elements
  • For Example:
    • A quarterly sales report generated from monthly data for the entire year
Thinking About the Problem
Back SMYC Forward
  • What's Needed:
    • Method parameters that describe the subset of interest
  • Possible Approaches:
    • An int[] containing the indexes to use
    • A conformal boolean[]
    • A conformal indicator (i.e., 0-1) array
  • The Most Common Problems:
    • The subset is contiguous so both of these approaches are more complicated than necessary
Using an Array of Indexes
Back SMYC Forward
javaexamples/programmingpatterns/subarrays/Subarrays_IndexArray.java (Fragment: 1)
 
javaexamples/programmingpatterns/subarrays/Subarrays_IndexArray.java (Fragment: 0)
 
Using a Conformal boolean[]
Back SMYC Forward
javaexamples/programmingpatterns/subarrays/Subarrays_BooleanArray.java (Fragment: 1)
 
javaexamples/programmingpatterns/subarrays/Subarrays_BooleanArray.java (Fragment: 0)
 
Using an Array of Indicators
Back SMYC Forward
javaexamples/programmingpatterns/subarrays/Subarrays_IndicatorArray.java (Fragment: 1)
 
javaexamples/programmingpatterns/subarrays/Subarrays_IndicatorArray.java (Fragment: 0)
 
The Pattern
Back SMYC Forward
  • The Idea:
    • Add two int parameters, the index to start with and the size of the subset
    • Include an overloaded convenience method with the original parameters
  • Terminology:
    • The index to start with is called the offset (from 0)
    • The size of the subset is called the length
The Pattern (cont.)
Back SMYC Forward

The Parameters for the Second Quarter of a Year of Monthly Data

Subarrays
An Example
Back SMYC Forward
javaexamples/programmingpatterns/subarrays/Subarrays.java (Fragment: 1)
 
javaexamples/programmingpatterns/subarrays/Subarrays.java (Fragment: 0)
 
Some Important Observations
Back SMYC Forward
  • Recall:
    • Arrays are reference types in Java so a method can change the elements of an array it is passed
  • Using the Subarray Pattern:
    • A method can change some or all of the elements of an array
  • A Related Programming Pattern:
    • Outbound Parameters
Examples in the Java Libraries (cont.)
Back SMYC Forward
  • The Arrays Class:
    • The sorting and searching methods
    • the fill() methods
    • The copyOfRange() method
  • The System Class:
    • The arraycopy() method
Arrays of Arrays
Back SMYC Forward
  • Recall:
    • Java doesn't really have multi-dimensional arrays (as some languages do) it has arrays of arrays
  • A Special Case:
    • When the arrays that are elements are all the same size (i.e., the array of arrays is rectangular)
An Example using a Rectangular Array of Arrays
Back SMYC Forward
javaexamples/programmingpatterns/subarrays/Subarrays.java (Fragment: 3)
 
javaexamples/programmingpatterns/subarrays/Subarrays.java (Fragment: 2)
 
There's Always More to Learn
Back -