NiraParikh 6 年之前
父節點
當前提交
f2b943f724

二進制
.DS_Store 查看文件


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

@@ -1,7 +1,85 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
+
4
+import com.sun.jmx.remote.internal.ArrayQueue;
5
+
6
+import java.lang.reflect.Array;
7
+import java.util.*;
8
+import java.util.function.Consumer;
9
+
10
+import static jdk.nashorn.internal.objects.ArrayBufferView.length;
11
+
3 12
 /**
4 13
  * Created by leon on 3/6/18.
5 14
  */
6
-public class ArrayUtility {
15
+
16
+public class  ArrayUtility <T>{
17
+
18
+     T [] list; // generic array of name list
19
+
20
+   public ArrayUtility ( T[] list){        //constructor
21
+
22
+       this.list = list;}
23
+
24
+
25
+    public Integer getNumberOfOccurrences (T element){
26
+        Integer count = 0;
27
+
28
+        for (T el: list){  //for each loop
29
+            if (el.equals(element)){
30
+                count++; // how many number of times the particular element occur
31
+            }
32
+
33
+        }
34
+        return count;
35
+
36
+        }
37
+
38
+        public Object[] merge(T[] arr1) {
39
+            Object[] mergeArr = new Object[list.length + arr1.length];
40
+            int counter = 0;
41
+            for (T el: list ) {             // copying element from list to mergeArr
42
+                mergeArr[counter] = el;
43
+                counter++;                  //need to know where elements r stored in index so next arr1 can take over merging
44
+            }
45
+            for(T el: arr1){
46
+                mergeArr[counter] = el;     // copying element from arr1 to mergeArr
47
+                counter++;
48
+            }
49
+            return mergeArr;
50
+        }
51
+
52
+    public Integer countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
53
+       ArrayUtility arrayUtility = new ArrayUtility(merge(arrayToMerge)); // creating new list to add changes to the merge
54
+        return arrayUtility.getNumberOfOccurrences(valueToEvaluate);
55
+    }
56
+
57
+    public T getMostCommonFromMerge(T[] arrayToMerge) {
58
+        ArrayUtility arrayUtil = new ArrayUtility(merge(arrayToMerge)); // creating new list to add changes to the merge
59
+
60
+        int tempCount = 0;
61
+        T mostCommonObj = null;  //made generics cuzz not sure wht type we have
62
+        for (Object el: arrayUtil.list) { //checking to see which el has most occurrance
63
+            if (getNumberOfOccurrences((T)el) > tempCount){ //counting duplicates but not conseqitive dupli
64
+                tempCount =getNumberOfOccurrences((T) el);
65
+                mostCommonObj = (T) el;
66
+            }
67
+        }
68
+        return mostCommonObj;
69
+    }
70
+
71
+    public T[] removeValue(T valueToRemove) {
72
+        ArrayList <T> tempArr = new ArrayList<>();
73
+        for (int i = 0; i< list.length; i ++){
74
+            if (!list[i].equals(valueToRemove)) {//objects have to do .equals
75
+                tempArr.add(list[i]);
76
+            }
77
+
78
+        }
79
+     int newLength =tempArr.size();
80
+        list = (T[]) Array.newInstance(list.getClass().getComponentType(), newLength); // can't create generic array
81
+    return tempArr.toArray(list);
82
+}
7 83
 }
84
+
85
+

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

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