Brandon Defrancis 6 年之前
父節點
當前提交
45699b7fae

+ 6
- 0
pom.xml 查看文件

@@ -21,6 +21,12 @@
21 21
             <artifactId>junit</artifactId>
22 22
             <version>4.12</version>
23 23
         </dependency>
24
+        <dependency>
25
+            <groupId>com.zipcodewilmington</groupId>
26
+            <artifactId>arrayutility</artifactId>
27
+            <version>1.0-SNAPSHOT</version>
28
+            <scope>test</scope>
29
+        </dependency>
24 30
     </dependencies>
25 31
 
26 32
 

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

@@ -1,7 +1,72 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
+import java.lang.reflect.Array;
4
+import java.util.ArrayList;
5
+
3 6
 /**
4 7
  * Created by leon on 3/6/18.
5 8
  */
6
-public class ArrayUtility {
9
+public class ArrayUtility<T> {
10
+
11
+    T[] list;
12
+
13
+    public ArrayUtility(T[] list) {
14
+        this.list = list;
15
+    }
16
+
17
+    public int getNumberOfOccurrences(T element) {
18
+        int count = 0;
19
+        for (T el : list){
20
+            if (el.equals(element)){
21
+                count++;
22
+            }
23
+        }
24
+        return count;
25
+    }
26
+
27
+    public Object[] merge(T[] arr){
28
+        int counter = 0;
29
+        Object[] mergearr = new Object[list.length + arr.length];
30
+        for (T el : list ){
31
+            mergearr[counter] = el;
32
+            counter++;
33
+        }
34
+        for (T el: arr){
35
+            mergearr[counter] = el;
36
+            counter++;
37
+        }
38
+        return mergearr;
39
+    }
40
+
41
+    public int countDuplicatesInMerge(T[] arrayUtility, T valueToEvaluate) {
42
+        ArrayUtility arrayUtil = new ArrayUtility(merge(arrayUtility));
43
+        return arrayUtil.getNumberOfOccurrences(valueToEvaluate);
44
+    }
45
+
46
+
47
+    public T getMostCommonFromMerge(T[] arrayToMerge) {
48
+        ArrayUtility newArr = new ArrayUtility(merge(arrayToMerge));
49
+        int count = 0;
50
+        T mostCommon = null;
51
+        for (Object el : newArr.list){
52
+            if (getNumberOfOccurrences((T)el) > count){
53
+                count = getNumberOfOccurrences((T)el);
54
+                mostCommon=(T)el;
55
+            }
56
+        }
57
+        return mostCommon;
58
+    }
59
+
60
+    public T[] removeValue(T valueToRemove) {
61
+    ArrayList<T> newArr = new ArrayList<>();//[list.length - getNumberOfOccurrences(valueToRemove)];
62
+        for (int i = 0; i < list.length; i++) {
63
+            if (!list[i].equals(valueToRemove)){
64
+                newArr.add(list[i]);
65
+            }
66
+        }
67
+        int newLength = newArr.size();
68
+        list = (T[]) Array.newInstance(list.getClass().getComponentType(), newLength);
69
+
70
+        return newArr.toArray(list);
71
+    }
7 72
 }

+ 0
- 2
src/test/java/com/zipcodewilmington/arrayutility/CountDuplicatesInMergeTest.java 查看文件

@@ -1,8 +1,6 @@
1 1
 package com.zipcodewilmington.arrayutility;
2
-
3 2
 import org.junit.Assert;
4 3
 import org.junit.Test;
5
-
6 4
 /**
7 5
  * Created by leon on 3/1/18.
8 6
  * The purpose of this class is to thoroughly test the method countDuplicatesInMerge()

+ 0
- 1
src/test/java/com/zipcodewilmington/arrayutility/GetMostCommonFromMergeTest.java 查看文件

@@ -1,5 +1,4 @@
1 1
 package com.zipcodewilmington.arrayutility;
2
-
3 2
 import org.junit.Assert;
4 3
 import org.junit.Test;
5 4
 

+ 0
- 1
src/test/java/com/zipcodewilmington/arrayutility/GetNumberOfOccurrencesTest.java 查看文件

@@ -1,5 +1,4 @@
1 1
 package com.zipcodewilmington.arrayutility;
2
-
3 2
 import org.junit.Assert;
4 3
 import org.junit.Test;
5 4
 

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

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