Browse Source

finshed string util

Shivam Patel 8 years ago
parent
commit
31462d7c57
2 changed files with 36 additions and 8 deletions
  1. 34
    6
      StringArrayUtils.java
  2. 2
    2
      package.bluej

+ 34
- 6
StringArrayUtils.java View File

@@ -188,13 +188,13 @@ public class StringArrayUtils {
188 188
             if(!(excluded[newArrIndex-1]).equals(array[i])){
189 189
                 excluded[newArrIndex] = array[i];
190 190
                 newArrIndex++;
191
-                
191
+
192 192
             }    
193 193
         }
194
-        
194
+
195 195
         String[] newArr = new String[newArrIndex];
196 196
         System.arraycopy(excluded, 0, newArr, 0, newArrIndex);
197
-        
197
+
198 198
         return newArr;
199 199
 
200 200
     }
@@ -205,10 +205,38 @@ public class StringArrayUtils {
205 205
      *  occurrence concatenated as a single string in an array of Strings
206 206
      */ // TODO
207 207
     public static String[] packConsecutiveDuplicates(String[] array) {
208
-        String[] newArr = new String[array.length]; 
209
-        
208
+
209
+        StringBuilder build = new StringBuilder(array[0]);
210
+
211
+        String[] newArr = new String[array.length-count(array)]; 
212
+        int counter = 0; 
213
+        for(int i = 1; i<array.length; i++){
214
+            if(array[i-1].equals(array[i])){
215
+                build.append(array[i]);
216
+
217
+            }
218
+            else{
219
+                newArr[counter] = build.toString();
220
+                build.setLength(0);
221
+                build.append(array[i]);
222
+                counter++;
223
+            }
224
+        }
210 225
         
211
-        return null;
226
+        newArr[counter] = build.toString();
227
+        return newArr;
228
+    }
229
+
230
+    public static int count(String[] array){
231
+        int count = 0;
232
+        for(int i = 1; i<array.length; i++){
233
+            if(array[i-1].equals(array[i])) {
234
+
235
+                count++;
236
+            }
237
+        }
238
+
239
+        return count;
212 240
     }
213 241
 
214 242
 }

+ 2
- 2
package.bluej View File

@@ -4,8 +4,8 @@ dependency1.to=StringArrayUtils
4 4
 dependency1.type=UsesDependency
5 5
 editor.fx.0.height=722
6 6
 editor.fx.0.width=800
7
-editor.fx.0.x=-79
8
-editor.fx.0.y=-818
7
+editor.fx.0.x=801
8
+editor.fx.0.y=22
9 9
 objectbench.height=101
10 10
 objectbench.width=776
11 11
 package.divider.horizontal=0.6