Selaa lähdekoodia

tests passed - had to comment out UnitTestingUtils in removevaluetest

Jennifer Chao 6 vuotta sitten
vanhempi
commit
732a8068d9

+ 73
- 1
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java Näytä tiedosto

@@ -1,7 +1,79 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
+import java.lang.reflect.Array;
4
+
3 5
 /**
4 6
  * Created by leon on 3/6/18.
5 7
  */
6
-public class ArrayUtility {
8
+public class ArrayUtility<T> {
9
+
10
+    private T[] array;
11
+
12
+    public ArrayUtility(T[] inputArray) {
13
+        this.array = inputArray;
14
+    }
15
+
16
+    public T[] mergeArrays(T[] inputArray, T[] arrayToMerge) {
17
+        T[] mergedArray = (T[]) Array.newInstance(this.array.getClass().getComponentType(), this.array.length + arrayToMerge.length);
18
+
19
+        for (int i = 0; i < mergedArray.length; i++) {
20
+            if (i < inputArray.length) {
21
+                mergedArray[i] = inputArray[i];
22
+            } else {
23
+                mergedArray[i] = arrayToMerge[i - inputArray.length];
24
+            }
25
+        }
26
+        return mergedArray;
27
+    }
28
+
29
+    public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
30
+        T[] mergedArray = mergeArrays(this.array, arrayToMerge);
31
+        int count = 0;
32
+
33
+        for (T element : mergedArray) {
34
+            if (element == valueToEvaluate) {
35
+                count++;
36
+            }
37
+        }
38
+        return count;
39
+    }
40
+
41
+    public T getMostCommonFromMerge(T[] arrayToMerge) {
42
+        T[] mergedArray = mergeArrays(this.array, arrayToMerge);
43
+        int mostCommonCount = 0;
44
+        T mostCommon = mergedArray[0];
45
+
46
+        for (int i = 0; i < mergedArray.length; i++) {
47
+            if (getNumberOfOccurrences(mergedArray[i]) > mostCommonCount) {
48
+                mostCommonCount = getNumberOfOccurrences(mergedArray[i]);
49
+                mostCommon = mergedArray[i];
50
+            }
51
+        }
52
+        return mostCommon;
53
+    }
54
+
55
+    public Integer getNumberOfOccurrences(T valueToEvaluate) {
56
+        int count = 0;
57
+
58
+        for (T element : this.array) {
59
+            if (element == valueToEvaluate) {
60
+                count++;
61
+            }
62
+        }
63
+        return count;
64
+    }
65
+
66
+    public T[] removeValue(T valueToRemove) {
67
+        T[] newArrayWithout = (T[]) Array.newInstance(this.array.getClass().getComponentType(), this.array.length - getNumberOfOccurrences(valueToRemove));
68
+        int j = 0;
69
+
70
+        for (int i = 0; i < this.array.length; i++) {
71
+            if (this.array[i] != valueToRemove) {
72
+                newArrayWithout[j] = this.array[i];
73
+                j++;
74
+            }
75
+        }
76
+        return newArrayWithout;
77
+    }
78
+
7 79
 }

+ 1
- 1
src/test/java/com/zipcodewilmington/arrayutility/RemoveValueTest.java Näytä tiedosto

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