Parcourir la source

This could be better...probably

Kthomas il y a 6 ans
Parent
révision
0d641e77fd

+ 113
- 1
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java Voir le fichier

@@ -1,7 +1,119 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
+import java.util.Arrays;
4
+
3 5
 /**
4 6
  * Created by leon on 3/6/18.
5 7
  */
6
-public class ArrayUtility {
8
+public class ArrayUtility<T> {
9
+    private T[] inputArray;
10
+
11
+    public ArrayUtility(T[] input) {
12
+        this.inputArray = input;
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
+        }
21
+
22
+        Integer resultCount = 0;
23
+        for (int x = 0; x < mergedArray.length; x++) {
24
+            if (mergedArray[x] == valueToEvaluate) {
25
+                resultCount++;
26
+            }
27
+        }
28
+
29
+        return resultCount;
30
+    }
31
+
32
+    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
39
+        this.inputArray = mergedArray;
40
+        T mostCommonOccurrence = mergedArray[0];
41
+        int maxOccurrence = 0;
42
+        for (T value : inputArray) {
43
+            if (this.getNumberOfOccurrences(value) > maxOccurrence) {
44
+                maxOccurrence = this.getNumberOfOccurrences(value);
45
+                mostCommonOccurrence = value;
46
+                }
47
+            }
48
+
49
+            return mostCommonOccurrence;
50
+        }
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
52
+    public Integer getNumberOfOccurrences(T valueToEvaluate) {
53
+        int count = 0;
54
+        for (T value : this.inputArray) {
55
+            if (value.equals(valueToEvaluate)) {
56
+                count++;
57
+            }
58
+        }
59
+
60
+        return count;
61
+    }
62
+
63
+    public T[] removeValue(T valueToRemove) {
64
+        T[] outputArray = Arrays.copyOf(this.inputArray, 0);
65
+        for (int i = 0; i < inputArray.length; i++) {
66
+            if (!inputArray[i].equals(valueToRemove)) {
67
+                outputArray = Arrays.copyOf(outputArray,outputArray.length + 1);
68
+                outputArray[outputArray.length - 1] = inputArray[i];
69
+            }
70
+        }
71
+
72
+        return outputArray;
73
+    }
7 74
 }
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
+//}

+ 0
- 1
src/test/java/com/zipcodewilmington/arrayutility/RemoveValueTest.java Voir le fichier

@@ -1,6 +1,5 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
-import com.zipcodewilmington.UnitTestingUtils;
4 3
 import org.junit.Test;
5 4
 
6 5
 /**