Преглед изворни кода

methods written, tests passed

jacob andersen пре 6 година
родитељ
комит
eb41fdd176

+ 85
- 1
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java Прегледај датотеку

@@ -1,7 +1,91 @@
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[] array;
12
+
13
+    public ArrayUtility(T[] array)
14
+
15
+    {
16
+    this.array=array;
17
+    }
18
+
19
+    public int getNumberOfOccurrences(T value)
20
+    {
21
+        int count = 0;
22
+        for (T v:array)
23
+        {
24
+            if (v.equals(value))
25
+            {
26
+                count++;
27
+            }
28
+        }
29
+
30
+    return count;
31
+    }
32
+
33
+    public Object[] merge(T[]array2)
34
+    {
35
+        Object[]mergeArr = new Object[array.length+array2.length];
36
+        int count=0;
37
+        for (T v:array)
38
+        {
39
+            mergeArr[count]=v;
40
+            count++;
41
+        }
42
+        for (T v:array2)
43
+        {
44
+            mergeArr[count]=v;
45
+            count++;
46
+        }
47
+        return mergeArr;
48
+    }
49
+    public int countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate)
50
+    {   ArrayUtility arrayUtil = new ArrayUtility(merge(arrayToMerge));
51
+
52
+        return arrayUtil.getNumberOfOccurrences(valueToEvaluate);
53
+    }
54
+
55
+    public T getMostCommonFromMerge(T[] arrayToMerge) {
56
+
57
+        ArrayUtility arrayUtil = new ArrayUtility(merge(arrayToMerge));
58
+        int tempCount = 0;
59
+        T mostCommonObj = null;
60
+        for(Object v:arrayUtil.array)
61
+        {
62
+            if(getNumberOfOccurrences((T)v)>tempCount)
63
+            {
64
+                tempCount=getNumberOfOccurrences((T)v);
65
+                mostCommonObj=(T)v;
66
+            }
67
+        }
68
+return mostCommonObj;
69
+    }
70
+
71
+
72
+
73
+    public T[] removeValue(T value)
74
+    {
75
+       ArrayList <T> newarr = new ArrayList<>();
76
+       for(int i=0;i<array.length;i++)
77
+       {
78
+           if(!array[i].equals(value))
79
+           {
80
+               newarr.add(array[i]);
81
+           }
82
+        }
83
+        int newlength = newarr.size();
84
+        array=(T[])Array.newInstance(array.getClass().getComponentType(),newlength);
85
+        return newarr.toArray(array);
86
+
87
+
88
+    }
89
+
90
+
7 91
 }

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