소스 검색

My brain started working..this commit is better

Kthomas 6 년 전
부모
커밋
ed51e99cab
1개의 변경된 파일14개의 추가작업 그리고 63개의 파일을 삭제
  1. 14
    63
      src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java

+ 14
- 63
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java 파일 보기

@@ -11,31 +11,30 @@ public class ArrayUtility<T> {
11 11
     public ArrayUtility(T[] input) {
12 12
         this.inputArray = input;
13 13
     }
14
-    //same as normal method used at the bottom but with a generic object of type T
15
-    public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
16
-        T[] mergedArray = Arrays.copyOf(inputArray, inputArray.length);
17
-        for (int i = inputArray.length; i < (arrayToMerge.length + inputArray.length); i++) {
18
-            mergedArray = Arrays.copyOf(mergedArray, mergedArray.length + 1);
19
-            mergedArray[i] = arrayToMerge[i - inputArray.length];
20
-        }
14
+    //created method to strictly merge arrays, (I remembered Single responsibility hehe)
15
+    public static <T> T[] mergeArrays(T[] objectArray, T[] objectArrayToAdd) {
16
+        T[] resultOfMergingTwoArrays = Arrays.copyOf(objectArray,
17
+                objectArray.length + objectArrayToAdd.length);
18
+        System.arraycopy(objectArrayToAdd, 0, resultOfMergingTwoArrays,
19
+                objectArray.length, objectArrayToAdd.length);
20
+        return resultOfMergingTwoArrays;
21
+    }
21 22
 
23
+    public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
24
+        mergeArrays(arrayToMerge, inputArray);
25
+        T[] mergedArray = mergeArrays(arrayToMerge, inputArray);
22 26
         Integer resultCount = 0;
23 27
         for (int x = 0; x < mergedArray.length; x++) {
24 28
             if (mergedArray[x] == valueToEvaluate) {
25 29
                 resultCount++;
26 30
             }
27 31
         }
28
-
29 32
         return resultCount;
30 33
     }
31 34
 
32 35
     public T getMostCommonFromMerge(T[] arrayToMerge) {
33
-        T[] mergedArray = Arrays.copyOf(inputArray, inputArray.length); //empty array that will hold the value of my new merged array once the logic has been done
34
-        for (int i = inputArray.length; i < (arrayToMerge.length + inputArray.length); i++) { //for loop iterating through entire length of both arrays
35
-            mergedArray = Arrays.copyOf(mergedArray, mergedArray.length + 1);
36
-            mergedArray[i] = arrayToMerge[i - inputArray.length];
37
-        }
38
-        //after merging using the above syntax now I want to find the most common occurrence from my new merged array using the method
36
+        mergeArrays(arrayToMerge,inputArray);
37
+        T[] mergedArray = mergeArrays(arrayToMerge, inputArray);
39 38
         this.inputArray = mergedArray;
40 39
         T mostCommonOccurrence = mergedArray[0];
41 40
         int maxOccurrence = 0;
@@ -45,10 +44,9 @@ public class ArrayUtility<T> {
45 44
                 mostCommonOccurrence = value;
46 45
                 }
47 46
             }
48
-
49 47
             return mostCommonOccurrence;
50 48
         }
51
-        //I could have also used this method below on both arrays above and compared the maxnumbofOccurrences on both arrays and still got my result
49
+
52 50
     public Integer getNumberOfOccurrences(T valueToEvaluate) {
53 51
         int count = 0;
54 52
         for (T value : this.inputArray) {
@@ -56,7 +54,6 @@ public class ArrayUtility<T> {
56 54
                 count++;
57 55
             }
58 56
         }
59
-
60 57
         return count;
61 58
     }
62 59
 
@@ -68,52 +65,6 @@ public class ArrayUtility<T> {
68 65
                 outputArray[outputArray.length - 1] = inputArray[i];
69 66
             }
70 67
         }
71
-
72 68
         return outputArray;
73 69
     }
74 70
 }
75
-//      normal method to get numbofoccurrecences, almost identical except my generic of type 'T' is now my object
76
-//      my objects now no longer need to be cast using the above method
77
-//    public static Integer getNumberOfOccurrences(Object[] objectArray, Object objectToCount) {
78
-//        Integer count = 0;
79
-//        for (int i = 0; i < objectArray.length; i++) {
80
-//            if (objectArray[i].equals(objectToCount)) {
81
-//                count++;
82
-//            }
83
-//        }
84
-//        return count;
85
-//    }
86
-//    public static <T> T[] mergeArrays(T[] objectArray, T[] objectArrayToAdd) {
87
-//
88
-//        T[] resultOfMergingTwoArrays = Arrays.copyOf(objectArray,
89
-//                objectArray.length + objectArrayToAdd.length);
90
-//        System.arraycopy(objectArrayToAdd, 0, resultOfMergingTwoArrays,
91
-//                objectArray.length, objectArrayToAdd.length);
92
-//
93
-//        return resultOfMergingTwoArrays;
94
-//    }
95
-//          this is the normal method, object is now  of type 'T' as a generic
96
-//    public static Integer[] mergeArrays(Object[] objectArray, Object[] objectArrayToAdd) {
97
-//        Integer[] resultArr = new Integer[objectArray.length + objectArrayToAdd.length];
98
-//        System.arraycopy(objectArray, 0, resultArr, 0, objectArray.length);
99
-//        System.arraycopy(objectArrayToAdd, 0, resultArr, objectArray.length, objectArrayToAdd.length);
100
-//        return resultArr;
101
-//    }
102
-//
103
-//    public static Object getMostCommon(Object[] objectArray) {
104
-//        Integer count = 0;
105
-//        Object obj = null;
106
-//        for (int i = 0; i < objectArray.length; i++) {
107
-//            Integer n = getNumberOfOccurrences(objectArray, objectArray[i]);
108
-//            if (n > count) {
109
-//                count = n;
110
-//                obj = objectArray[i];
111
-//            }
112
-//        }
113
-//        return obj;
114
-//    }
115
-//  why did intelliJ suggest making me have to cast everything to T...none of these test would pass in this case.
116
-//    public T countDuplicatesInMerge(Integer[] arrayToMerge, T valueToEvaluate) {
117
-//        return null;
118
-//    }
119
-//}