Subarrays
A Programming Pattern |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
for
loop (because it's determinate)length
attribute/** * 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; }
/** * 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; }
/** * 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); }
boolean[]
/** * 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; }
/** * 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); }
/** * 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; }
/** * 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); }
int
parameters, the index to start
with and the size of the subsetoffset
(from 0)length
The Parameters for the Second Quarter of a Year of Monthly Data
/** * 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; }
/** * 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); }
/** * 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; }
/** * 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); }