Demetrius Murray 6 jaren geleden
bovenliggende
commit
2453ed6448

+ 53
- 1
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java Bestand weergeven

@@ -1,7 +1,59 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
+import java.lang.reflect.Array;
4
+import java.util.*;
5
+
3 6
 /**
4 7
  * Created by leon on 3/6/18.
5 8
  */
6
-public class ArrayUtility {
9
+public class ArrayUtility<T> {
10
+    private List<T> list;
11
+    private T[] inputArray;
12
+
13
+    public ArrayUtility(T[] inputArray){
14
+        this.list = new ArrayList<>(Arrays.asList(inputArray));
15
+        this.inputArray = inputArray;
16
+    }
17
+
18
+    public void merge(T[] arrayToMerge){
19
+        list.addAll(new ArrayList<>(Arrays.asList(arrayToMerge)));
20
+    }
21
+
22
+    public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate){
23
+        merge(arrayToMerge);
24
+        return getNumberOfOccurrences(valueToEvaluate);
25
+    }
26
+
27
+    public T getMostCommonFromMerge(T[] arrayToMerge) {
28
+        merge(arrayToMerge);
29
+        int most = 0;
30
+        T mostCommon = list.get(0);
31
+        for (T t : list){
32
+            int count = getNumberOfOccurrences(t);
33
+            if (count>most) {
34
+                most = count;
35
+                mostCommon = t;
36
+            }
37
+        }
38
+        return mostCommon;
39
+    }
40
+
41
+    public Integer getNumberOfOccurrences(T valueToEvaluate) {
42
+        int count = 0;
43
+        for (T t : list) {
44
+            if (t.equals(valueToEvaluate)) count++;
45
+        }
46
+        return count;
47
+    }
48
+
49
+    public T[] removeValue(T valueToRemove) {
50
+        list.removeIf(e -> e.equals(valueToRemove));
51
+
52
+        Class tClass = inputArray.getClass();
53
+        Class compType = tClass.getComponentType();
54
+        T[] array = (T[]) Array.newInstance(compType,(list.size()-getNumberOfOccurrences(valueToRemove)));
55
+        for(int i=0; i<array.length; i++)
56
+            array[i] = list.get(i);
57
+        return array;
58
+    }
7 59
 }

+ 1
- 0
src/test/java/com/zipcodewilmington/arrayutility/ArrayUtilityTestSuite.java Bestand weergeven

@@ -16,4 +16,5 @@ import org.junit.runners.Suite;
16 16
         RemoveValueTest.class
17 17
 })
18 18
 public class ArrayUtilityTestSuite {
19
+
19 20
 }

+ 0
- 1
src/test/java/com/zipcodewilmington/arrayutility/RemoveValueTest.java Bestand weergeven

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