Przeglądaj źródła

ArrayUtility DONE

Nathan Hall 6 lat temu
rodzic
commit
e342172ccb

+ 78
- 1
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java Wyświetl plik

@@ -1,7 +1,84 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
+import com.sun.tools.classfile.Instruction;
4
+
5
+import java.lang.reflect.Array;
6
+import java.util.ArrayList;
7
+import java.util.Arrays;
8
+import java.util.HashMap;
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
+    public ArrayUtility(T[] inputArray) {
16
+        this.inputArray = inputArray;
17
+    }
18
+    T[] inputArray;
19
+
20
+
21
+
22
+    public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
23
+
24
+        ArrayUtility au = new ArrayUtility(merge(arrayToMerge));
25
+
26
+
27
+
28
+
29
+        return au.getNumberOfOccurrences(valueToEvaluate);
30
+    }
31
+
32
+    public Object[] merge(T[] arrayToMerge){
33
+        Object[] mergedArray = new Object [arrayToMerge.length + inputArray.length];
34
+               System.arraycopy(inputArray, 0, mergedArray, 0, inputArray.length );
35
+               System.arraycopy(arrayToMerge, 0, mergedArray, inputArray.length, arrayToMerge.length );
36
+
37
+    return mergedArray;
38
+    }
39
+
40
+    public T[] removeValue(T valueToRemove) {
41
+        ArrayList<T> Jae = new ArrayList<>();
42
+
43
+        for (int i = 0; i < inputArray.length; i++){
44
+            if (!inputArray[i].equals(valueToRemove)){
45
+                Jae.add(inputArray[i]);
46
+            }
47
+        }
48
+
49
+        int length = Jae.size();
50
+        inputArray = (T[]) Array.newInstance(inputArray.getClass().getComponentType(), length);
51
+
52
+        return Jae.toArray(inputArray);
53
+    }
54
+
55
+    public Integer getNumberOfOccurrences(T valueToEvaluate) {
56
+        Integer count = 0;
57
+
58
+        for (int i = 0; i < inputArray.length; i++){
59
+            if (inputArray[i].equals(valueToEvaluate)){
60
+                count++;
61
+            }
62
+        }
63
+
64
+        return count;
65
+    }
66
+
67
+    public T getMostCommonFromMerge(T[] arrayToMerge) {
68
+
69
+        ArrayUtility au = new ArrayUtility(merge(arrayToMerge));
70
+        Object[] merged = merge(arrayToMerge);
71
+//        HashMap<T, Integer> map = new HashMap<>();
72
+        int count = 0;
73
+        T popular = null;
74
+
75
+        for (Object o : merged){
76
+            if (getNumberOfOccurrences((T)o) > count){
77
+                count = getNumberOfOccurrences((T)o);
78
+                popular = (T) o;
79
+            }
80
+        }
81
+        return popular;
82
+
83
+    }
7 84
 }

+ 0
- 1
src/test/java/com/zipcodewilmington/arrayutility/RemoveValueTest.java Wyświetl plik

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