Quellcode durchsuchen

3 out of four methods down need to do a basic remove using arrays.copy

Joshua Wurdemann vor 6 Jahren
Ursprung
Commit
d55c0fe8c5

+ 60
- 1
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java Datei anzeigen

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

+ 1
- 1
src/test/java/com/zipcodewilmington/arrayutility/RemoveValueTest.java Datei anzeigen

@@ -1,6 +1,6 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
-import com.zipcodewilmington.UnitTestingUtils;
3
+
4 4
 import org.junit.Test;
5 5
 
6 6
 /**