nafis nibir před 8 roky
rodič
revize
f86d2207b9
2 změnil soubory, kde provedl 33 přidání a 11 odebrání
  1. 26
    11
      StringArrayUtils.java
  2. 7
    0
      StringArrayUtilsTest.java

+ 26
- 11
StringArrayUtils.java Zobrazit soubor

@@ -145,26 +145,41 @@ public class StringArrayUtils {
145 145
         return revisedArray;
146 146
     }
147 147
 
148
+    public static String[] sortArray(String[] array){
149
+        String[] sorted = new String[array.length];
150
+        Arrays.sort(sorted);
151
+        return sorted;
152
+    }
153
+    
148 154
     /**
149 155
      * @param array array of chars
150 156
      * @return array of Strings with consecutive duplicates removes
151 157
      */ // TODO
152 158
     public static String[] removeConsecutiveDuplicates(String[] array) {
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
-            }
159
+        int dupes = countDuplicates(array);
160
+        String[] revisedArr = new String[array.length-dupes];
161
+        int j = 0;
162
+        for(int i = 0; i < array.length-1; i++){
163
+            if (array[i]!=(array[i+1])){
164
+                revisedArr[j] = array[i];
165
+                j++;
166
+            } 
158 167
         }
159
-        removeValue(array, null);
160
-        return array;
168
+        revisedArr[revisedArr.length-1]=array[array.length-1];
169
+        return revisedArr;
161 170
     }
162 171
     
163
-    public static String[] sortArray(String[] array){
164
-        
165
-        
172
+    public static int countDuplicates(String[] array){
173
+        String[] newArray = array;   
174
+        int counter = 0;
175
+        for(int i = 0; i < newArray.length-1; i++){
176
+            if (newArray[i].equals(newArray[i+1])){
177
+                counter++;   
178
+            }
179
+        }
180
+        return counter;
166 181
     }
167
-
182
+    
168 183
     /**
169 184
      * @param array array of chars
170 185
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings

+ 7
- 0
StringArrayUtilsTest.java Zobrazit soubor

@@ -366,7 +366,14 @@ public class StringArrayUtilsTest {
366 366
 
367 367
 
368 368
 
369
+    @Test
370
+    public void testCountDuplicates(){
371
+        String[] array = {"aba", "aba", "baa", "bab", "bab", "dog"};
372
+        int expected = 2;
373
+        int actual = StringArrayUtils.countDuplicates(array);
374
+        Assert.assertEquals(actual,expected);
369 375
 
376
+    }
370 377
 
371 378
 
372 379