Sfoglia il codice sorgente

last method with array list

mpierse 6 anni fa
parent
commit
baf3a16d27
1 ha cambiato i file con 28 aggiunte e 13 eliminazioni
  1. 28
    13
      src/main/java/com/zipcodewilmington/StringArrayUtils.java

+ 28
- 13
src/main/java/com/zipcodewilmington/StringArrayUtils.java Vedi File

@@ -1,5 +1,6 @@
1 1
 package com.zipcodewilmington;
2 2
 
3
+import java.util.ArrayList;
3 4
 import java.util.Arrays;
4 5
 
5 6
 import static java.util.Arrays.binarySearch;
@@ -180,23 +181,37 @@ return resultArr;
180 181
      */ // TODO
181 182
     public static String[] packConsecutiveDuplicates(String[] array) {
182 183
 
183
-        String[] tempArr = new String[array.length];
184
+     ArrayList<String> resultList = new ArrayList<String>();
185
+     String tempString = "";
186
+        for (int i = 0; i < array.length - 1; i++) {
187
+            if (array[i].equals(array[i + 1])) {
188
+                tempString += array[i];
189
+                resultList.add(i, tempString);
190
+            } else {
191
+                resultList.add(array[i]);
192
+            }
193
+        }
194
+       String[] resultArray = resultList.toArray();
195
+        return resultArray;
196
+    }
197
+
198
+      /* String[] tempArr = new String[array.length];
184 199
         String tempString = "";
185 200
         int counter = 0;
186 201
         int decrement = 0;
187 202
 
188
-        for (int i=0; i<array.length-1; i++) {
203
+        for (int i = 0; i < array.length - 1; i++) {
189 204
             tempString = array[i];
190
-            while (array[i].equals(array[i++])) {
205
+            while (array[i].equals(array[i+1])) {
191 206
                 tempString += array[i];
192
-                tempArr[counter++] = tempString;
193
-            }
194
-        } else if (array[i].equals(array[i-1]))
195
-            { tempString = "";
196
-                continue;
197
-            } else {
198
-                tempArr[counter++]=array[i];
207
+                tempArr[counter] = tempString;
199 208
             }
209
+          if (array[i].equals(array[i - 1])) {
210
+            tempString = "";
211
+            continue;
212
+        } else {
213
+            tempArr[counter++] = array[i];
214
+        }
200 215
 
201 216
         }
202 217
         for (int i=0; i<tempArr.length; i++){
@@ -204,12 +219,12 @@ return resultArr;
204 219
             { decrement = i;
205 220
                 break;}
206 221
         }
207
-        String[] resultArr= Arrays.copyOf(tempArr, decrement+1);
222
+        String[] resultArr= Arrays.copyOf(tempArr, decrement);
208 223
         resultArr[resultArr.length-1] = array[array.length-1];
209 224
         System.out.print(resultArr);
210 225
         return resultArr;
211 226
 
212
-    }
213
-
227
+    }*/
214 228
 
215 229
 }
230
+