瀏覽代碼

Merge 4f58c4323b12667212a929a7f599ed6ad2698aea into 5706be95854cd14023a646b5a08307e1b69df29c

KATRINAHIGH 6 年之前
父節點
當前提交
28458ea8e5
沒有帳戶連結到提交者的電子郵件

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

@@ -1,7 +1,83 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
+import java.lang.reflect.Array;
4
+import java.lang.reflect.ParameterizedType;
5
+import java.lang.reflect.Type;
6
+import java.util.ArrayList;
7
+import java.util.Arrays;
8
+
3 9
 /**
4 10
  * Created by leon on 3/6/18.
5 11
  */
6
-public class ArrayUtility {
12
+public class ArrayUtility<T> {
13
+
14
+    private T[] inputArray;
15
+
16
+    public ArrayUtility(T[] input) {
17
+        this.inputArray = input;
18
+    }
19
+
20
+    private T[] concatenate(T[] arrayToMerge) {
21
+        T[] result = Arrays.copyOf(this.inputArray, this.inputArray.length + arrayToMerge.length);
22
+        System.arraycopy(arrayToMerge, 0, result, this.inputArray.length, arrayToMerge.length);
23
+        return result;
24
+    }
25
+
26
+    private Integer getNumberOfOccurrences(T[] array, T valueToEvaluate) {
27
+        Integer count = 0;
28
+        for (T item : array) {
29
+            if (item == valueToEvaluate) {
30
+                count++;
31
+            }
32
+        }
33
+        return count;
34
+    }
35
+
36
+    public Integer getNumberOfOccurrences(T valueToEvaluate) {
37
+         return getNumberOfOccurrences(this.inputArray, valueToEvaluate);
38
+    }
39
+
40
+    public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
41
+        T[] result = concatenate(arrayToMerge);
42
+        return getNumberOfOccurrences(result, valueToEvaluate);
43
+    }
44
+
45
+
46
+    public T getMostCommonFromMerge(T[] arrayToMerge) {
47
+        T[] mergedArray = concatenate(arrayToMerge);
48
+        Integer count = 0;
49
+        T result = null;
50
+        for(T item : mergedArray){
51
+            int n = getNumberOfOccurrences(mergedArray, item);
52
+            if(n > count){
53
+                count = n;
54
+                result = item;
55
+            }
56
+        }
57
+        return result;
58
+    }
59
+
60
+
61
+    public T[] removeValue(T valueToRemove) {
62
+        int newSize = inputArray.length - getNumberOfOccurrences(valueToRemove);
63
+        T[] outPutArray = Arrays.copyOf(this.inputArray, newSize);
64
+        int outPutIndex = 0;
65
+        for (T item : this.inputArray) {
66
+            if (!item.equals(valueToRemove)) {
67
+                outPutArray[outPutIndex] = item;
68
+                outPutIndex++;
69
+            }
70
+        }
71
+        return outPutArray;
72
+    }
73
+
7 74
 }
75
+
76
+/** My orginal solutions to these problems involved converting an array to an arraylist and back to an array.
77
+ * This posed an issue when attempting to solve the problems with generics. Generics are covariant. Meaning
78
+ * at compile time an array requires a known type value. Whereas generics do not. Therefore, it is not
79
+ * possible to instantiate a generic array. As a result, I went down a rabbit hole  attempting to instantiate a
80
+ * generic array inside .toArray(). Uugh, reflection and yada yada. In the end the solution involved declaring a
81
+ * property of type T[] and then setting that value in the constructor. So now the generic array is instantiated
82
+ * outside of the class. When you use arrays.copyOf it doesn't care about the type...this method just looks at
83
+ * the memory and makes a copy of it. No need to convert an arraylist to an array and try to instantiate it.*/

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

@@ -1,7 +1,8 @@
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
+import org.junit.Assert;
5 6
 
6 7
 /**
7 8
  * Created by leon on 3/1/18.