瀏覽代碼

Merge a9fad0b314a9cde71f34a8094b19c99b6f3af036 into 5706be95854cd14023a646b5a08307e1b69df29c

ktecle 6 年之前
父節點
當前提交
1342e62163
沒有帳戶連結到提交者的電子郵件

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

@@ -1,7 +1,69 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
+import java.util.ArrayList;
4
+import java.util.Arrays;
5
+import java.util.Collections;
6
+
3 7
 /**
4 8
  * Created by leon on 3/6/18.
5 9
  */
6
-public class ArrayUtility {
10
+public class ArrayUtility<T> {
11
+
12
+    private T [] myArray;
13
+    private  ArrayList<T> myArrayList;
14
+    T[] resultArray;
15
+
16
+    public ArrayUtility(T[] myArray) {
17
+        this.myArray = myArray;
18
+        myArrayList = new ArrayList<>();
19
+        Collections.addAll(myArrayList,myArray);
20
+    }
21
+
22
+    public Integer countDuplicatesInMerge(T[]arrayToMerge, T valueToEvaluate){
23
+
24
+        Collections.addAll(myArrayList,arrayToMerge);
25
+        Integer count =0;
26
+        for(int i =0;i<myArrayList.size();i++){
27
+                if(myArrayList.get(i)==valueToEvaluate){
28
+                    count++;
29
+                }
30
+            }
31
+
32
+        return count;
33
+    }
34
+
35
+    public T getMostCommonFromMerge(T[]arrayToMerge){
36
+
37
+        Collections.addAll(myArrayList,arrayToMerge);
38
+        T mostCommon =myArrayList.get(0);
39
+        for(int i=0;i<myArrayList.size();i++){
40
+            if(getNumberOfOccurrences(myArrayList.get(i))>getNumberOfOccurrences(mostCommon)){
41
+                mostCommon=myArrayList.get(i);
42
+            }
43
+        }
44
+
45
+        return mostCommon;
46
+    }
47
+
48
+    public Integer getNumberOfOccurrences(T valueToEvaluate){
49
+        int count=0;
50
+        for(int i=0;i<myArrayList.size();i++){
51
+            if(myArrayList.get(i)==valueToEvaluate){
52
+                count++;
53
+            }
54
+        }
55
+        return count;
56
+    }
57
+
58
+    public T[] removeValue(T valueToRemove){
59
+        int resultArrayIndex=0;
60
+        resultArray = Arrays.copyOf(myArray,myArray.length-getNumberOfOccurrences(valueToRemove));
61
+        for(int i=0;i<myArray.length;i++){
62
+            if(!myArray[i].equals(valueToRemove)){
63
+                resultArray[resultArrayIndex]=myArray[i];
64
+                resultArrayIndex++;
65
+            }
66
+        }
67
+        return resultArray;
68
+    }
7 69
 }

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

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