William Simkins 6 vuotta sitten
vanhempi
commit
d357bae5c6

+ 74
- 1
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java Näytä tiedosto

@@ -1,7 +1,80 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
+
4
+import com.sun.tools.javac.util.ArrayUtils;
5
+
6
+import java.lang.reflect.Array;
7
+import java.util.*;
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[] inputArray){
17
+        this.inputArray = inputArray;
18
+    }
19
+
20
+    public Object[] merge(T[] arrayToMerge, T[] inputArray) {
21
+        ArrayList<T> t1 = new ArrayList<>(Arrays.asList(arrayToMerge));
22
+        t1.addAll(Arrays.asList(inputArray));
23
+        return t1.toArray(new Object[t1.size()]);
24
+    }
25
+
26
+    public T[] removeValue(T valueToRemove) {
27
+            int counter = 0;
28
+            for(int i = 0; i < inputArray.length; i++){
29
+                if(inputArray[i]!= valueToRemove){
30
+                    inputArray[counter] = inputArray[i];
31
+                    counter++;
32
+                }
33
+            }
34
+            return Arrays.copyOf(inputArray, counter);
35
+        }
36
+
37
+    public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
38
+        Object[] mergedArray = merge(arrayToMerge, inputArray);
39
+        int counter = 0;
40
+        for(int i = 0; i < mergedArray.length; i++)
41
+        {
42
+            if(mergedArray[i].equals(valueToEvaluate))
43
+            {
44
+                counter++;
45
+            }
46
+        }
47
+        return counter;
48
+    }
49
+
50
+    public T getMostCommonFromMerge(T[] arrayToMerge) {
51
+        merge(arrayToMerge, inputArray);
52
+        int count = 0, tempCount;
53
+        T common = arrayToMerge[0];
54
+        T temp = null;
55
+        for (int i = 0; i < (arrayToMerge.length - 1); i++) {
56
+            temp = arrayToMerge[i];
57
+            tempCount = 0;
58
+            for (int j = i + 1; j < arrayToMerge.length; j++) {
59
+                if (temp == arrayToMerge[j])
60
+                    tempCount++;
61
+            }
62
+            if (tempCount > count) {
63
+                common = temp;
64
+                count = tempCount;
65
+            }
66
+        }
67
+        return common;
68
+    }
69
+
70
+    public Integer getNumberOfOccurrences(T valueToEvaluate) {
71
+        int counter = 0;
72
+        for(int i = 0; i < inputArray.length; i++){
73
+            if (valueToEvaluate == inputArray[i]){
74
+                counter++;
75
+            }
76
+        }
77
+        return counter;
78
+    }
79
+
7 80
 }

+ 1
- 1
src/test/java/com/zipcodewilmington/arrayutility/RemoveValueTest.java Näytä tiedosto

@@ -1,6 +1,6 @@
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 5
 
6 6
 /**