JMU
Subarrays
A Programming Pattern


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Motivation
Review
Review (cont.)
javaexamples/programmingpatterns/subarrays/SubarraysMotivation.java (Fragment: 0)
    /**
     * A "traditional" total() method that is passed an array.
     *
     * @param  data  The data array
     * @return The sum of the elements in the array
     */
    public static int total(int[] data) {
        int result;
        result = 0;

        for (int i = 0; i < data.length; ++i) {
            result += data[i];
        }
        return result;
    }
        
Review (cont.)
Thinking About the Problem
Using an Array of Indexes
javaexamples/programmingpatterns/subarrays/Subarrays_IndexArray.java (Fragment: 1)
    /**
     * A more flexible total() method that potentially operates
     * on only a part of the array.
     *
     * @param  data    The data array
     * @param  indexes The indexes to operate on
     * @return The sum of the specified elements in the array
     */
    public static double total(double[] data, int[] indexes) {
        double result;

        result = 0;        
        for (int i = 0; i < indexes.length; ++i) {
            result += data[indexes[i]];
        }
        return result;
    }
        
javaexamples/programmingpatterns/subarrays/Subarrays_IndexArray.java (Fragment: 0)
    /**
     * An overloaded total() method that is passed an array.
     *
     * @param  data  The data array
     * @return The sum of the elements in the array
     */
    public static double total(double[] data) {
        int[] indexes = new int[data.length];
        for (int i=0; i<indexes.length; ++i) indexes[i] = i;
        return total(data, indexes);
    }
        
Using a Conformal boolean[]
javaexamples/programmingpatterns/subarrays/Subarrays_BooleanArray.java (Fragment: 1)
    /**
     * A more flexible total() method that potentially operates
     * on only a part of the array.
     *
     * @param  data The data array
     * @param  use  A conformal array of boolean value
     * @return The sum of the specified elements in the array
     */
    public static double total(double[] data, boolean[] use) {
        double result;

        result = 0;        
        for (int i = 0; i < data.length; ++i) {
            if (use[i]) result += data[i];
        }
        return result;
    }
        
javaexamples/programmingpatterns/subarrays/Subarrays_BooleanArray.java (Fragment: 0)
    /**
     * An overloaded total() method that is passed an array.
     *
     * @param  data  The data array
     * @return The sum of the elements in the array
     */
    public static double total(double[] data) {
        boolean[] use = new boolean[data.length];
        Arrays.fill(use, true);
        return total(data, use);
    }
        
Using an Array of Indicators
javaexamples/programmingpatterns/subarrays/Subarrays_IndicatorArray.java (Fragment: 1)
    /**
     * A more flexible total() method that potentially operates
     * on only a part of the array.
     *
     * @param  data The   data array
     * @param  indicator  A conformal indicator array of 0-1 values
     * @return The sum of the specified elements in the array
     */
    public static double total(double[] data, int[] indicator) {
        double result;

        result = 0;        
        for (int i = 0; i < data.length; ++i) {
            result += indicator[i] * data[i];
        }
        return result;
    }
        
javaexamples/programmingpatterns/subarrays/Subarrays_IndicatorArray.java (Fragment: 0)
    /**
     * An overloaded total() method that is passed an array.
     *
     * @param  data  The data array
     * @return The sum of the elements in the array
     */
    public static double total(double[] data) {
        int[] indicator = new int[data.length];
        Arrays.fill(indicator, 1);
        return total(data, indicator);
    }
        
The Pattern
The Pattern (cont.)

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

images/Subarrays.svg
An Example
javaexamples/programmingpatterns/subarrays/Subarrays.java (Fragment: 1)
    /**
     * A more flexible total() method that potentially operates
     * on only a part of the array.
     *
     * @param  data    The data array
     * @param  offset  The first element to operate on
     * @param  length  The number of elements to operate on
     * @return The sum of the elements in the array
     */
    public static double total(double[] data, int offset, int length) {
        double result;

        result = 0;        
        for (int i = offset; i < offset + length; ++i) {
            result += data[i];
        }
        return result;
    }
        
javaexamples/programmingpatterns/subarrays/Subarrays.java (Fragment: 0)
    /**
     * An overloaded total() method that is passed an array.
     *
     * @param  data  The data array
     * @return The sum of the elements in the array
     */
    public static double total(double[] data) {
        return total(data, 0, data.length);
    }
        
Some Important Observations
Examples in the Java Libraries (cont.)
Arrays of Arrays
An Example using a Rectangular Array of Arrays
javaexamples/programmingpatterns/subarrays/Subarrays.java (Fragment: 3)
    /**
     * A more flexible total() method that is passed a
     * rectangular array of arrays.
     *
     * @param  data  The data array
     * @param  roffset   The first row to operate on
     * @param  coffset   The first column to operate on
     * @param  rlength   The number of rows to operate on
     * @param  clength   The number of columns to operate on
     * @return The sum of the elements in the array
     */
    public static double total(double[][] data, 
                               int roffset, int coffset, 
                               int rlength, int clength) {
        double result;
        
        result = 0;        
        for (int r = roffset; r < roffset + rlength; ++r) {
            for (int c = coffset; c < coffset + clength; ++c) {
                result += data[r][c];
            }
        }
        return result;
    }
        
javaexamples/programmingpatterns/subarrays/Subarrays.java (Fragment: 2)
    /**
     * An overloaded total() method that is passed a rectangular 
     * array of arrays.
     *
     * @param  data  The data array
     * @return The sum of the elements in the array
     */
    public static double total(double[][] data) {
        return total(data, 0, 0, data.length, data[0].length);
    }