Parcourir la source

Lab Submission

Rachelle il y a 8 ans
Parent
révision
1340729579
2 fichiers modifiés avec 49 ajouts et 17 suppressions
  1. 47
    16
      StringArrayUtils.java
  2. 2
    1
      StringArrayUtilsTest.java

+ 47
- 16
StringArrayUtils.java Voir le fichier

@@ -152,23 +152,54 @@ public class StringArrayUtils {
152 152
      * @return array of Strings with consecutive duplicates removes
153 153
      */ // TODO
154 154
     public static String[] removeConsecutiveDuplicates(String[] array) {
155
-        String[] sortedArray = array.sort();
156
-        
157
-        for(int i = 1; i < sortedArray.length; i++){
158
-            if(sortedArray[i]!=sortedArray[i-1]){
159
-                    add to array list
155
+        int newArrayLength = array.length - countDuplicates(array);
156
+        String[] newArray = new String[newArrayLength];
157
+        newArray[0] = array[0];
158
+        int index = 1;
159
+        for(int i = 1; i < array.length; i++){
160
+            if(!array[i].equals((array[i-1]))){
161
+                newArray[index] = array[i];
162
+                index += 1;
160 163
             }
161 164
         }
165
+        return newArray;
162 166
     }
163
-    return null;
164
-}
165 167
 
166
-/**
167
- * @param array array of chars
168
- * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
169
- */ // TODO
170
-public static String[] packConsecutiveDuplicates(String[] array) {
171
-return null;
172
-}
173
-
174
-}
168
+    public static int countDuplicates(String[] array){
169
+        int duplicates = 0;
170
+        for (int i =1; i< array.length; i++){
171
+            if(array[i].equals(array[i-1])){
172
+                duplicates++;
173
+            }
174
+        }
175
+        return duplicates;
176
+    }
177
+
178
+    /**
179
+     * @param array array of chars
180
+     * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
181
+     */ // TODO
182
+    public static String[] packConsecutiveDuplicates(String[] array) {
183
+        int newArrayLength = array.length-countDuplicates(array);
184
+        String [] newArray = new String [newArrayLength];
185
+        
186
+        StringBuilder tempString = new StringBuilder(array[0]);
187
+        int index = 0;
188
+        
189
+        for (int i = 1; i< array.length; i++){
190
+           
191
+           if(array[i].equals(array[i-1])){
192
+               tempString.append(array[i]);
193
+            }else{
194
+                newArray[index] = tempString.toString();
195
+                index++;
196
+                tempString.delete(0,tempString.length());
197
+                tempString.append(array[i]);
198
+            }
199
+        } 
200
+        newArray[index] = tempString.toString();
201
+        return newArray;
202
+    }
203
+    
204
+
205
+}

+ 2
- 1
StringArrayUtilsTest.java Voir le fichier

@@ -1,5 +1,5 @@
1 1
  
2
-
2
+import java.util.Arrays;
3 3
 import org.junit.Assert;
4 4
 import org.junit.Test;
5 5
 
@@ -397,6 +397,7 @@ public class StringArrayUtilsTest {
397 397
         String[] array = {"a", "a", "a", "b", "c", "c", "a", "a", "d"};
398 398
         String[] expected = {"aaa", "b", "cc", "aa", "d"};
399 399
         String[] actual = StringArrayUtils.packConsecutiveDuplicates(array);
400
+        System.out.print(Arrays.toString(actual));
400 401
         Assert.assertEquals(expected, actual);
401 402
     }
402 403