diff --git a/Week9/Assignment9/bin/ArrayUtility.class b/Week9/Assignment9/bin/ArrayUtility.class index 6bcbcad..aeab885 100644 Binary files a/Week9/Assignment9/bin/ArrayUtility.class and b/Week9/Assignment9/bin/ArrayUtility.class differ diff --git a/Week9/Assignment9/src/ArrayUtility.java b/Week9/Assignment9/src/ArrayUtility.java index 588e69b..b9ece20 100644 --- a/Week9/Assignment9/src/ArrayUtility.java +++ b/Week9/Assignment9/src/ArrayUtility.java @@ -11,15 +11,15 @@ public class ArrayUtility { * @return 1D array of combine all elements from 2D array */ public static int[] merge(int[][] d) { - int length = 0; - for(int i = 0;i < d.length;i++)length+=d[i].length; - int[] merged_array = new int[length]; - int current_index = 0; - for(int i = 0;i < d.length;i++)for(int j = 0;j < d[i].length;j++) { - merged_array[current_index] = d[i][j]; - current_index++; + int length = 0; //Initialize length to store the column as we will pass this to the array constructor + for(int i = 0;i < d.length;i++)length+=d[i].length; //SUM of X(nY) = length + int[] mergedArray = new int[length]; // Create a new array with size equal to length + int currentIndex = 0; //Create a new variable to store the 2D Array Index + for(int i = 0;i < d.length;i++)for(int j = 0;j < d[i].length;j++) { //Loop through every Row and In each iteration of row, loop through every column + mergedArray[currentIndex] = d[i][j]; //Set the 2D array element CI to 3D's IJ + currentIndex++; //Move the cursor to the next element } - return merged_array; + return mergedArray; } /** @@ -32,10 +32,10 @@ public class ArrayUtility { */ public static int[][] split(int[] d, int n) { if(n<=0) return new int[0][0]; //Return to prevent ArithmeticException: / by zero and negative array index - if (d.length%n!=0)return new int[0][0]; //Return, not array is not splittable - int[][] splitted_array = new int[n][d.length/n]; //Create the splitted_array array and initialize it - for(int i = 0;i < n;i++)for(int j = 0;j