Seth 6 vuotta sitten
vanhempi
commit
546ff9b1c5

+ 20
- 2
src/main/java/com/zipcodewilmington/arrayutility/ArrayUtility.java Näytä tiedosto

@@ -1,5 +1,6 @@
1 1
 package com.zipcodewilmington.arrayutility;
2 2
 
3
+import java.lang.reflect.Array;
3 4
 import java.util.ArrayList;
4 5
 import java.util.Arrays;
5 6
 
@@ -8,11 +9,12 @@ import java.util.Arrays;
8 9
  */
9 10
 public class ArrayUtility<T> {
10 11
     ArrayList<T> utility;
11
-
12
+    private T[] array;
12 13
 
13 14
 
14 15
     public ArrayUtility(T[] array) {
15 16
         this.utility = new ArrayList<T>(Arrays.asList(array));
17
+        this.array = array;
16 18
 
17 19
     }
18 20
 
@@ -46,5 +48,21 @@ public class ArrayUtility<T> {
46 48
         return mostCommon;
47 49
     }
48 50
 
49
-    
51
+    public int countDuplicatesInMerge(T[] arrayToMerge, T valueToEvaluate) {
52
+      merge(arrayToMerge);
53
+      int counter = getNumberOfOccurrences(valueToEvaluate);
54
+      return counter;
55
+    }
56
+
57
+    public T[] removeValue(T valueToRemove) {
58
+        ArrayList<T> newList = new ArrayList<>();
59
+        for (T each: utility) {
60
+            if (!each.equals(valueToRemove)) {
61
+                newList.add(each);
62
+            }
63
+        }
64
+        int newLength = newList.size();
65
+        T[] arr = (T[]) Array.newInstance(array.getClass().getComponentType(), newLength);
66
+        return newList.toArray(arr);
67
+    }
50 68
 }

+ 65
- 65
src/test/java/com/zipcodewilmington/arrayutility/CountDuplicatesInMergeTest.java Näytä tiedosto

@@ -8,69 +8,69 @@ import org.junit.Test;
8 8
  * The purpose of this class is to thoroughly test the method countDuplicatesInMerge()
9 9
  */
10 10
 public class CountDuplicatesInMergeTest {
11
-//    @Test
12
-//    public void integerTest() {
13
-//        // Given
14
-//        Integer valueToEvaluate = 7;
15
-//        Integer expected = 5;
16
-//        Integer[] inputArray = {1, 2, valueToEvaluate, 8, 4, 5, valueToEvaluate, 0, 9, 8, valueToEvaluate};
17
-//        Integer[] arrayToMerge = {1, 2, valueToEvaluate, 8, 4, 5, valueToEvaluate, 0, 9, 8};
18
-//        ArrayUtility<Integer> arrayUtility = new ArrayUtility<Integer>(inputArray);
19
-//
20
-//        // When
21
-//        Integer actual = arrayUtility.countDuplicatesInMerge(arrayToMerge, valueToEvaluate);
22
-//
23
-//        // Then
24
-//        Assert.assertEquals(expected, actual);
25
-//    }
26
-//
27
-//    @Test
28
-//    public void longTest() {
29
-//        // Given
30
-//        Long valueToEvaluate = 7L;
31
-//        Integer expected = 6;
32
-//        Long[] inputArray = {1L, 2L, valueToEvaluate, 8L, 4L, 5L, valueToEvaluate, 0L, 9L, 8L, valueToEvaluate};
33
-//        Long[] arrayToMerge = {1L, 2L, valueToEvaluate, 8L, 4L, 5L, valueToEvaluate, 0L, 9L, 8L, valueToEvaluate};
34
-//        ArrayUtility<Long> arrayUtility = new ArrayUtility<Long>(inputArray);
35
-//
36
-//        // When
37
-//        Integer actual = arrayUtility.countDuplicatesInMerge(arrayToMerge, valueToEvaluate);
38
-//
39
-//        // Then
40
-//        Assert.assertEquals(expected, actual);
41
-//    }
42
-//
43
-//    @Test
44
-//    public void stringTest() {
45
-//        // Given
46
-//        String valueToEvaluate = "a";
47
-//        Integer expected = 7;
48
-//        String[] inputArray = {"1", "2", valueToEvaluate, "8", "4", "5", valueToEvaluate, "0", "9", "8", valueToEvaluate};
49
-//        String[] arrayToMerge = {"1", "2", valueToEvaluate, "8", "4", "5", valueToEvaluate, "0", "9", "8", valueToEvaluate, valueToEvaluate};
50
-//
51
-//        ArrayUtility<String> arrayUtility = new ArrayUtility<String>(inputArray);
52
-//
53
-//        // When
54
-//        Integer actual = arrayUtility.countDuplicatesInMerge(arrayToMerge, valueToEvaluate);
55
-//
56
-//        // Then
57
-//        Assert.assertEquals(expected, actual);
58
-//    }
59
-//
60
-//    @Test
61
-//    public void objectTest() {
62
-//        // Given
63
-//        Object valueToEvaluate = "a";
64
-//        Integer expected = 8;
65
-//        Object[] inputArray = {"1", "2", valueToEvaluate, "8", "4", "5", valueToEvaluate, "0", "9", "8", valueToEvaluate};
66
-//        Object[] arrayToMerge = {"1", "2", valueToEvaluate, "8", "4", "5", valueToEvaluate, "0", "9", "8", valueToEvaluate, valueToEvaluate, valueToEvaluate};
67
-//
68
-//        ArrayUtility<Object> arrayUtility = new ArrayUtility<Object>(inputArray);
69
-//
70
-//        // When
71
-//        Integer actual = arrayUtility.countDuplicatesInMerge(arrayToMerge, valueToEvaluate);
72
-//
73
-//        // Then
74
-//        Assert.assertEquals(expected, actual);
75
-//    }
11
+    @Test
12
+    public void integerTest() {
13
+        // Given
14
+        Integer valueToEvaluate = 7;
15
+        Integer expected = 5;
16
+        Integer[] inputArray = {1, 2, valueToEvaluate, 8, 4, 5, valueToEvaluate, 0, 9, 8, valueToEvaluate};
17
+        Integer[] arrayToMerge = {1, 2, valueToEvaluate, 8, 4, 5, valueToEvaluate, 0, 9, 8};
18
+        ArrayUtility<Integer> arrayUtility = new ArrayUtility<Integer>(inputArray);
19
+
20
+        // When
21
+        Integer actual = arrayUtility.countDuplicatesInMerge(arrayToMerge, valueToEvaluate);
22
+
23
+        // Then
24
+        Assert.assertEquals(expected, actual);
25
+    }
26
+
27
+    @Test
28
+    public void longTest() {
29
+        // Given
30
+        Long valueToEvaluate = 7L;
31
+        Integer expected = 6;
32
+        Long[] inputArray = {1L, 2L, valueToEvaluate, 8L, 4L, 5L, valueToEvaluate, 0L, 9L, 8L, valueToEvaluate};
33
+        Long[] arrayToMerge = {1L, 2L, valueToEvaluate, 8L, 4L, 5L, valueToEvaluate, 0L, 9L, 8L, valueToEvaluate};
34
+        ArrayUtility<Long> arrayUtility = new ArrayUtility<Long>(inputArray);
35
+
36
+        // When
37
+        Integer actual = arrayUtility.countDuplicatesInMerge(arrayToMerge, valueToEvaluate);
38
+
39
+        // Then
40
+        Assert.assertEquals(expected, actual);
41
+    }
42
+
43
+    @Test
44
+    public void stringTest() {
45
+        // Given
46
+        String valueToEvaluate = "a";
47
+        Integer expected = 7;
48
+        String[] inputArray = {"1", "2", valueToEvaluate, "8", "4", "5", valueToEvaluate, "0", "9", "8", valueToEvaluate};
49
+        String[] arrayToMerge = {"1", "2", valueToEvaluate, "8", "4", "5", valueToEvaluate, "0", "9", "8", valueToEvaluate, valueToEvaluate};
50
+
51
+        ArrayUtility<String> arrayUtility = new ArrayUtility<String>(inputArray);
52
+
53
+        // When
54
+        Integer actual = arrayUtility.countDuplicatesInMerge(arrayToMerge, valueToEvaluate);
55
+
56
+        // Then
57
+        Assert.assertEquals(expected, actual);
58
+    }
59
+
60
+    @Test
61
+    public void objectTest() {
62
+        // Given
63
+        Object valueToEvaluate = "a";
64
+        Integer expected = 8;
65
+        Object[] inputArray = {"1", "2", valueToEvaluate, "8", "4", "5", valueToEvaluate, "0", "9", "8", valueToEvaluate};
66
+        Object[] arrayToMerge = {"1", "2", valueToEvaluate, "8", "4", "5", valueToEvaluate, "0", "9", "8", valueToEvaluate, valueToEvaluate, valueToEvaluate};
67
+
68
+        ArrayUtility<Object> arrayUtility = new ArrayUtility<Object>(inputArray);
69
+
70
+        // When
71
+        Integer actual = arrayUtility.countDuplicatesInMerge(arrayToMerge, valueToEvaluate);
72
+
73
+        // Then
74
+        Assert.assertEquals(expected, actual);
75
+    }
76 76
 }

+ 66
- 66
src/test/java/com/zipcodewilmington/arrayutility/RemoveValueTest.java Näytä tiedosto

@@ -8,71 +8,71 @@ import org.junit.Test;
8 8
  * The purpose of this class is to thoroughly test the method removeValue()
9 9
  */
10 10
 public class RemoveValueTest {
11
-//    @Test
12
-//    public void integerTest() {
13
-//        // Given
14
-//        Integer valueToRemove = 7;
15
-//        Integer[] expected = {11, 2, 8, 4, 5, 0, 9, 8};
16
-//        Integer[] inputArray = {11, 2, valueToRemove, 8, 4, 5, valueToRemove, 0, 9, 8, valueToRemove};
17
-//        ArrayUtility<Integer> arrayUtility = new ArrayUtility<Integer>(inputArray);
18
-//
19
-//
20
-//        // When
21
-//        Integer[] actual = arrayUtility.removeValue(valueToRemove);
22
-//
23
-//        // Then
24
-//        UnitTestingUtils.assertArrayEquality(expected, actual);
25
-//    }
26
-//
27
-//    @Test
28
-//    public void longTest() {
29
-//        // Given
30
-//        Long valueToRemove = 7L;
31
-//        Long[] expected = {12L, 2L, 8L, 4L, 5L, 0L, 9L, 8L};
32
-//        Long[] inputArray = {12L, 2L, valueToRemove, 8L, 4L, 5L, valueToRemove, 0L, 9L, 8L, valueToRemove};
33
-//        ArrayUtility<Long> arrayUtility = new ArrayUtility<Long>(inputArray);
34
-//
35
-//
36
-//        // When
37
-//        Long[] actual = arrayUtility.removeValue(valueToRemove);
38
-//
39
-//        // Then
40
-//        UnitTestingUtils.assertArrayEquality(expected, actual);
41
-//    }
42
-//
43
-//
44
-//    @Test
45
-//    public void stringTest() {
46
-//        // Given
47
-//        String valueToRemove = "7";
48
-//        String[] expected = {"13", "2", "8", "4", "5", "0", "9", "8"};
49
-//        String[] inputArray = {"13", "2", valueToRemove, "8", "4", "5", valueToRemove, "0", "9", "8", valueToRemove};
50
-//        ArrayUtility<String> arrayUtility = new ArrayUtility<String>(inputArray);
51
-//
52
-//
53
-//        // When
54
-//        String[] actual = arrayUtility.removeValue(valueToRemove);
55
-//
56
-//        // Then
57
-//        UnitTestingUtils.assertArrayEquality(expected, actual);
58
-//    }
59
-//
60
-//
61
-//
62
-//    @Test
63
-//    public void objectTest() {
64
-//        // Given
65
-//        Object valueToRemove = "7";
66
-//        Object[] expected = {"41", "2", "8", "4", "5", "0", "9", "8"};
67
-//        Object[] inputArray = {"41", "2", valueToRemove, "8", "4", "5", valueToRemove, "0", "9", "8", valueToRemove};
68
-//        ArrayUtility<Object> arrayUtility = new ArrayUtility<Object>(inputArray);
69
-//
70
-//
71
-//        // When
72
-//        Object[] actual = arrayUtility.removeValue(valueToRemove);
73
-//
74
-//        // Then
75
-//        UnitTestingUtils.assertArrayEquality(expected, actual);
76
-//    }
11
+    @Test
12
+    public void integerTest() {
13
+        // Given
14
+        Integer valueToRemove = 7;
15
+        Integer[] expected = {11, 2, 8, 4, 5, 0, 9, 8};
16
+        Integer[] inputArray = {11, 2, valueToRemove, 8, 4, 5, valueToRemove, 0, 9, 8, valueToRemove};
17
+        ArrayUtility<Integer> arrayUtility = new ArrayUtility<Integer>(inputArray);
18
+
19
+
20
+        // When
21
+        Integer[] actual = arrayUtility.removeValue(valueToRemove);
22
+
23
+        // Then
24
+        UnitTestingUtils.assertArrayEquality(expected, actual);
25
+    }
26
+
27
+    @Test
28
+    public void longTest() {
29
+        // Given
30
+        Long valueToRemove = 7L;
31
+        Long[] expected = {12L, 2L, 8L, 4L, 5L, 0L, 9L, 8L};
32
+        Long[] inputArray = {12L, 2L, valueToRemove, 8L, 4L, 5L, valueToRemove, 0L, 9L, 8L, valueToRemove};
33
+        ArrayUtility<Long> arrayUtility = new ArrayUtility<Long>(inputArray);
34
+
35
+
36
+        // When
37
+        Long[] actual = arrayUtility.removeValue(valueToRemove);
38
+
39
+        // Then
40
+        UnitTestingUtils.assertArrayEquality(expected, actual);
41
+    }
42
+
43
+
44
+    @Test
45
+    public void stringTest() {
46
+        // Given
47
+        String valueToRemove = "7";
48
+        String[] expected = {"13", "2", "8", "4", "5", "0", "9", "8"};
49
+        String[] inputArray = {"13", "2", valueToRemove, "8", "4", "5", valueToRemove, "0", "9", "8", valueToRemove};
50
+        ArrayUtility<String> arrayUtility = new ArrayUtility<String>(inputArray);
51
+
52
+
53
+        // When
54
+        String[] actual = arrayUtility.removeValue(valueToRemove);
55
+
56
+        // Then
57
+        UnitTestingUtils.assertArrayEquality(expected, actual);
58
+    }
59
+
60
+
61
+
62
+    @Test
63
+    public void objectTest() {
64
+        // Given
65
+        Object valueToRemove = "7";
66
+        Object[] expected = {"41", "2", "8", "4", "5", "0", "9", "8"};
67
+        Object[] inputArray = {"41", "2", valueToRemove, "8", "4", "5", valueToRemove, "0", "9", "8", valueToRemove};
68
+        ArrayUtility<Object> arrayUtility = new ArrayUtility<Object>(inputArray);
69
+
70
+
71
+        // When
72
+        Object[] actual = arrayUtility.removeValue(valueToRemove);
73
+
74
+        // Then
75
+        UnitTestingUtils.assertArrayEquality(expected, actual);
76
+    }
77 77
 
78 78
 }