nafis nibir 8 år sedan
förälder
incheckning
b80ea68535
2 ändrade filer med 35 tillägg och 11 borttagningar
  1. 30
    3
      StringArrayUtils.java
  2. 5
    8
      StringArrayUtilsTest.java

+ 30
- 3
StringArrayUtils.java Visa fil

118
      * @return number of occurrences the specified `value` has occurred
118
      * @return number of occurrences the specified `value` has occurred
119
      */ // TODO
119
      */ // TODO
120
     public static int getNumberOfOccurrences(String[] array, String value) {
120
     public static int getNumberOfOccurrences(String[] array, String value) {
121
-        return 0;
121
+        int numOfOcc = 0;
122
+        for (String element: array){
123
+            if (element.equals(value)){
124
+                numOfOcc++;
125
+            }
126
+        }
127
+        return numOfOcc;
122
     }
128
     }
123
 
129
 
124
     /**
130
     /**
127
      * @return array with identical contents excluding values of `value`
133
      * @return array with identical contents excluding values of `value`
128
      */ // TODO
134
      */ // TODO
129
     public static String[] removeValue(String[] array, String valueToRemove) {
135
     public static String[] removeValue(String[] array, String valueToRemove) {
130
-        return null;
136
+        int howManyToDelete = getNumberOfOccurrences(array, valueToRemove);
137
+        String[] revisedArray = new String[array.length-howManyToDelete];
138
+        int index = 0;
139
+        for(String element: array){
140
+            if(element != valueToRemove){
141
+                revisedArray[index] = element;
142
+                index++;
143
+            }
144
+        }
145
+        return revisedArray;
131
     }
146
     }
132
 
147
 
133
     /**
148
     /**
135
      * @return array of Strings with consecutive duplicates removes
150
      * @return array of Strings with consecutive duplicates removes
136
      */ // TODO
151
      */ // TODO
137
     public static String[] removeConsecutiveDuplicates(String[] array) {
152
     public static String[] removeConsecutiveDuplicates(String[] array) {
138
-        return null;
153
+        
154
+        for(int i = 0; i < array.length-2; i++){
155
+            if (array[i].equals(array[i+1])){
156
+                array[i+1]= null;    
157
+            }
158
+        }
159
+        removeValue(array, null);
160
+        return array;
161
+    }
162
+    
163
+    public static String[] sortArray(String[] array){
164
+        
165
+        
139
     }
166
     }
140
 
167
 
141
     /**
168
     /**

+ 5
- 8
StringArrayUtilsTest.java Visa fil

280
 
280
 
281
     @Test
281
     @Test
282
     public void testContainsLetter(){
282
     public void testContainsLetter(){
283
-       boolean expected = true;
284
-       boolean given = StringArrayUtilis
285
-        
283
+       String[] array = {"a", "b", "c", "d"};
284
+       boolean given = StringArrayUtils.containsLetter(array);
285
+       Assert.assertFalse(given);
286
         
286
         
287
     }
287
     }
288
     
288
     
289
-    @Test
290
-    public void testMakeSplitStringArray(){
291
-        
292
-        
293
-    }
289
+    
290
+   
294
 
291
 
295
 
292
 
296
 
293