/** * This class contains experimental methods for summing a 2-dimensional * array of integers. * * @author alchambers * @version Spring 2016 */ public class ArrayExperiments{ /** * This method sums the values in a 2-dimensional array of integers. * It takes a standard approach which is to traverse the array row-wise: * i.e. sum row 0, then row 1, then row 2, then row 3, etc. * * @param array A 2-dimensional array of integers * @return The sum of the integers in the array */ private static int standardApproach(int[][] array){ int sum = 0; for(int i = 0; i < array.length; i++){ for(int n = 0; n < array[i].length; n++){ sum = sum + array[i][n]; } } return sum; } /** * This method attempts to sum the elements using a column-wise * traversal. The idea is to swap the order of the for loops * * @param array A 2-dimensional array of integers * @return The sum of the integers in the array */ private static int approach1(int[][] array){ int sum = 0; int i = 0; for(int n = 0; n < array[i].length; n++){ for(i = 0; i < array.length; i++){ System.out.println("[" + i + ", " + n + "]"); sum = sum + array[i][n]; } i = 0; } return sum; } /** * This method attempts to sum the elements using a column-wise * traversal. The idea is to swap the order in which we index * into the array * * @param array A 2-dimensional array of integers * @return The sum of the integers in the array */ private static int approach2(int[][] array){ int sum = 0; for(int i = 0; i < array.length; i++){ for(int n = 0; n < array[i].length; n++){ System.out.println("[" + n + ", " + i + "]"); sum = sum + array[n][i]; } } return sum; } /** * This method sums a 2-dimensional array of intgers using a * column-wise traversal. This method works even if the rows * have differing lengths. * * @param array A 2-dimensional array of integers * @return The sum of the integers in the array */ private static int approach3(int[][] array){ // First determine the maximum length for any row int maxLength = 0; for(int i = 0; i < array.length; i++){ if(array[i].length > maxLength){ maxLength = array[i].length; } } int sum = 0; // Note I switched to "row" and "col" instead of "i" and "j" // In this case, I felt "row" and "col" made the code clearer for(int col = 0; col < maxLength; col++){ for(int row = 0; row < array.length; row++){ if(col < array[row].length){ System.out.println("[" + row + ", " + col + "]"); sum = sum + array[row][col]; } } } return sum; } /** * Prints out a 2-dimensional array of integers * @param array The array to be printed */ private static void printArray(int[][] array){ for(int i = 0; i < array.length; i++){ for(int j = 0; j < array[i].length; j++){ System.out.print(array[i][j] + " "); } System.out.println(); } System.out.println(); } /** * Takes a 2-dimensional array of integers and invokes the different * approaches for summing the array. * @param array The array to be summed */ private static void runExperiments(int[][] array){ printArray(array); System.out.println("=== RUNNING EXPERIMENTS ==="); System.out.println("Standard approach: " + standardApproach(array)); System.out.println(); System.out.println("Approach 1: "); System.out.println(approach1(array)); System.out.println(); System.out.println("Approach 2: "); System.out.println(approach2(array)); System.out.println(); System.out.println("Approach 3: "); System.out.println(approach3(array)); System.out.println(); } public static void main(String[] args){ final int SIZE = 3; // Create an array where each row has the same length int[][] test = new int[SIZE][SIZE]; for(int i = 0; i < test.length; i++){ for(int j = 0; j < test[i].length; j++){ test[i][j] = i+1; } } // Create an array where each row has differing lengths int[][] test2 = new int[SIZE][]; for(int i = 0; i < test2.length; i++){ test2[i] = new int[i+1]; for(int j = 0; j < test2[i].length; j++){ test2[i][j] = i+1; } } // Uncomment to run experiments //runExperiments(test); //runExperiments(test2); } }