#22 shakila3

Отворено
shakila3 жели да споји 2 комит(е) из shakila3/CR-MesoLabs-Generics-ArrayUtility:master у master

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

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

+ 2
- 2
src/test/java/com/zipcodewilmington/arrayutility/GetMostCommonFromMergeTest.java Прегледај датотеку

@@ -7,7 +7,7 @@ import org.junit.Test;
7 7
  * Created by leon on 3/1/18.
8 8
  * The purpose of this class is to thoroughly test the method getMostCommonFromMerge()
9 9
  */
10
-public class GetMostCommonFromMergeTest {
10
+public class GetMostCommonFromMergeTest <T> {
11 11
     @Test
12 12
     public void integerTest() {
13 13
         // Given
@@ -69,4 +69,4 @@ public class GetMostCommonFromMergeTest {
69 69
         // Then
70 70
         Assert.assertEquals(expected, actual);
71 71
     }
72
-}
72
+}

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