瀏覽代碼

completed lab

Tommy Rogers 6 年之前
父節點
當前提交
22a170befe

+ 86
- 1
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java 查看文件

@@ -1,7 +1,92 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
+
4
+import java.lang.reflect.Array;
5
+import java.util.Arrays;
6
+import java.util.HashMap;
7
+import java.util.HashSet;
8
+import java.util.Set;
9
+
3 10
 /**
4 11
  * Created by leon on 3/6/18.
5 12
  */
6
-public class ArrayUtility {
13
+public class ArrayUtility <T> {
14
+
15
+    private T[] array;
16
+
17
+    public ArrayUtility(T[] array){
18
+        this.array = array;
19
+    }
20
+
21
+
22
+ public Integer countDuplicatesInMerge(T[] array, T value){
23
+        int count = getNumberOfOccurrences(value);
24
+        ArrayUtility<Object> arrayUtility = new ArrayUtility<Object>(array);
25
+        count += arrayUtility.getNumberOfOccurrences(value);
26
+     return count;
27
+ }
28
+
29
+
30
+
31
+
32
+    public  T[] removeValue(T value){
33
+        T[] array = Arrays.copyOf(this.array, this.array.length - getNumberOfOccurrences(value));
34
+        int x = 0;
35
+        for(T t : this.array)
36
+            if (!t.equals(value)) {
37
+                array[x] = t;
38
+                x++;
39
+            }
40
+        return array;
41
+    }
42
+
43
+
44
+    public T getMostCommonFromMerge(T[] arrayToMerge) {
45
+        int count;
46
+        HashMap<T, Integer> testMap = new HashMap<>();
47
+        for(T t: this.array) {
48
+            count = testMap.getOrDefault(t, 0);
49
+            testMap.put(t, ++count);
50
+        }
51
+        for(T t: arrayToMerge) {
52
+            count = testMap.getOrDefault(t, 0);
53
+            testMap.put(t, ++count);
54
+        }
55
+
56
+        count = 0;
57
+        T answer = null;
58
+        for(T t: testMap.keySet())
59
+            if(testMap.get(t) > count){
60
+            count = testMap.get(t);
61
+            answer =t;
62
+            }
63
+
64
+
65
+//        T [] mergedArray = mergeArrays(arrayToMerge);
66
+//        int count = 0;
67
+//        T answer = null;
68
+//        for(T t : mergedArray)
69
+//            if(getNumberOfOccurrences(t) > count) {
70
+//                count = getNumberOfOccurrences(t);
71
+//                answer = t;
72
+//            }
73
+        return answer;
74
+    }
75
+
76
+    public T [] mergeArrays(T[] arrayToMerge){
77
+        int thisALen = this.array.length;
78
+        int AToMergeLen = arrayToMerge.length;
79
+        T[] mergedArray = (T[]) Array.newInstance(arrayToMerge.getClass().getComponentType(), thisALen+ AToMergeLen);
80
+        System.arraycopy(this.array, 0, mergedArray, 0, thisALen);
81
+        System.arraycopy(arrayToMerge, 0, mergedArray, AToMergeLen, AToMergeLen);
82
+        return mergedArray;
83
+    }
84
+
85
+    public Integer getNumberOfOccurrences(T valueToEvaluate) {
86
+        int count = 0;
87
+        for(T t : this.array)
88
+            if (t.equals(valueToEvaluate))
89
+                count++;
90
+        return count;
91
+    }
7 92
 }

+ 0
- 1
src/test/java/com/zipcodewilmington/arrayutility/RemoveValueTest.java 查看文件

@@ -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
 /**