From bf69b7fc3b169923afefb03c6a2a9bb8c46cf737 Mon Sep 17 00:00:00 2001 From: Siwat Sirichai Date: Thu, 29 Oct 2020 13:43:27 +0700 Subject: [PATCH] Add ArrayUtility Comments --- Week9/Assignment9/bin/ArrayUtility.class | Bin 1702 -> 1699 bytes Week9/Assignment9/src/ArrayUtility.java | 24 +++++++++++------------ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Week9/Assignment9/bin/ArrayUtility.class b/Week9/Assignment9/bin/ArrayUtility.class index 6bcbcadab6f67ff166c79b21df01e63812648efa..aeab885f0efc97c05e9bf774c27fcf8eb92263b2 100644 GIT binary patch delta 54 zcmZ3+yO?*v0U7Sx)S~p%6vv{X#7ag6rf5$_2A<^7qN3Ei63>bE%(-}r3vx0`N`NXh JD=@aP0s!_L5)=Rc delta 57 zcmZ3?yNq|j0Xd%B)S~p%l=#G=qQpu@2Bv6FMh4#G(xRf&yps6LiFeGo`HBm2GD}K8 LYBtLLI_eXW 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