Kate Moore 6 yıl önce
ebeveyn
işleme
a36c4d176b

+ 82
- 1
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java Dosyayı Görüntüle

@@ -1,7 +1,88 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
+import java.lang.reflect.Array;
4
+import java.util.ArrayList;
5
+import java.util.Arrays;
6
+import java.util.List;
7
+import java.util.function.Consumer;
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[] array;
15
+
16
+    public ArrayUtility(T[] list) {
17
+        this.array = list;
18
+    }
19
+
20
+    public  T[] removeValue(T valueToRemove){
21
+        List<T> list = new ArrayList<T>(Arrays.asList(array));
22
+        List<T> newList = new ArrayList<>();
23
+        // this is lamda -----
24
+//        Consumer<T> consumer = (n) -> {
25
+//            if(n != valueToRemove) {
26
+//                newList.add(n);
27
+//            }
28
+//        };
29
+//        list.forEach(consumer);
30
+        for(T element: list) {
31
+            if(!element.equals(valueToRemove)) {
32
+                newList.add(element);
33
+            }
34
+        }
35
+        T[] objs = (T[])Array.newInstance(this.array.getClass().getComponentType(), newList.size());
36
+        return (T[]) newList.toArray(objs);
37
+
38
+    }
39
+
40
+
41
+
42
+    public int countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
43
+       ArrayUtility arrayUtility = new ArrayUtility(merge(arrayToMerge));
44
+        return arrayUtility.getNumberOfOccurrences(valueToEvaluate);
45
+    }
46
+
47
+    public Object[] merge(T[] arr2){
48
+        Object [] mergeArr = new Object[array.length + arr2.length];
49
+        int counter = 0;
50
+        for(T el : array) {
51
+            mergeArr[counter] = el;
52
+            counter++;
53
+        }
54
+        for(T el : arr2) {
55
+            mergeArr[counter] = el;
56
+            counter++;
57
+        }
58
+        return mergeArr;
59
+    }
60
+
61
+    public int getNumberOfOccurrences(T valueToEvaluate) {
62
+        int count = 0;
63
+        for(T el : array) {
64
+            if(el.equals(valueToEvaluate)) {
65
+                count++;
66
+            }
67
+        }
68
+        return count;
69
+    }
70
+
71
+
72
+
73
+    public T getMostCommonFromMerge(T[] arrayToMerge){
74
+        ArrayUtility arrayUtility = new ArrayUtility(merge(arrayToMerge));
75
+        int tempCount = 0;
76
+        T mostCommonEl = null;
77
+        for(Object el : arrayUtility.array) {
78
+            if(getNumberOfOccurrences((T)el) > tempCount) {
79
+                tempCount = getNumberOfOccurrences((T)el);
80
+                mostCommonEl = (T)el;
81
+            }
82
+        }
83
+        return mostCommonEl;
84
+    }
85
+
86
+
87
+
7 88
 }

+ 2
- 0
src/test/java/com/zipcodewilmington/arrayutility/ArrayUtilityTestSuite.java Dosyayı Görüntüle

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

+ 1
- 2
src/test/java/com/zipcodewilmington/arrayutility/RemoveValueTest.java Dosyayı Görüntüle

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