瀏覽代碼

completed string utils

Eric Foster 8 年之前
父節點
當前提交
eedd1c31ba
共有 2 個文件被更改,包括 41 次插入12 次删除
  1. 38
    9
      StringArrayUtils.java
  2. 3
    3
      package.bluej

+ 38
- 9
StringArrayUtils.java 查看文件

@@ -1,6 +1,7 @@
1 1
 import java.util.Arrays;
2 2
 import java.io.*;
3
-import java.util.*; 
3
+import java.util.*;
4
+import java.lang.StringBuilder;
4 5
 
5 6
 /**
6 7
  * Created by leon on 1/29/18.
@@ -84,7 +85,7 @@ public class StringArrayUtils {
84 85
      */ // TODO
85 86
     public static boolean isPangramic(String[] array) {
86 87
         String arrayString = Arrays.toString(array).toLowerCase();
87
-        
88
+
88 89
         if(arrayString.length() < 26){
89 90
             return false;
90 91
         }
@@ -94,7 +95,7 @@ public class StringArrayUtils {
94 95
                 return false;
95 96
             }
96 97
         }
97
-        
98
+
98 99
         return true;
99 100
     }
100 101
 
@@ -111,7 +112,7 @@ public class StringArrayUtils {
111 112
                 numberOfOccurrences += 1;
112 113
             }
113 114
         }
114
-        
115
+
115 116
         return numberOfOccurrences;
116 117
     }
117 118
 
@@ -123,7 +124,7 @@ public class StringArrayUtils {
123 124
     public static String[] removeValue(String[] array, String valueToRemove) {
124 125
         int numberOfOccurrences = getNumberOfOccurrences(array, valueToRemove);
125 126
         String[] newArray = new String[array.length - numberOfOccurrences];
126
-        
127
+
127 128
         int i = 0;
128 129
         for(String element : array){
129 130
             if(!element.equals(valueToRemove)){
@@ -133,7 +134,7 @@ public class StringArrayUtils {
133 134
         }
134 135
         return newArray;
135 136
     }
136
-    
137
+
137 138
     public static int numberOfConsecutiveDuplicates(String[] array) {
138 139
         int numberOfConsecDups = 0;
139 140
         for(int i=1; i<=array.length-1; i++){
@@ -149,7 +150,17 @@ public class StringArrayUtils {
149 150
      * @return array of Strings with consecutive duplicates removes
150 151
      */ // TODO
151 152
     public static String[] removeConsecutiveDuplicates(String[] array) {
152
-        
153
+        ArrayList<String> newList = new ArrayList<String>();
154
+        //add first element of array to new array
155
+        newList.add(array[0]);
156
+        //compare each element in your array starting with the second
157
+        for (int i=1; i<=array.length-1; i++){
158
+            //if an element does not equal an element before it, add to new array
159
+            if(!(array[i].equals(array[i-1]))){
160
+                newList.add(array[i]);
161
+            }
162
+        }
163
+        return newList.toArray(new String[0]);
153 164
     }
154 165
 
155 166
     /**
@@ -157,7 +168,25 @@ public class StringArrayUtils {
157 168
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
158 169
      */ // TODO
159 170
     public static String[] packConsecutiveDuplicates(String[] array) {
160
-        return null;
171
+        //create a new string using string builder
172
+        StringBuilder arrayAsString = new StringBuilder();
173
+        //add first element in array to new array string
174
+        arrayAsString.append(array[0]);
175
+        //look at each element in the array
176
+        for(int i=1; i<=array.length-1; i++){
177
+            //if the element in the array equals the previous element, add it to
178
+            //   the string
179
+            if(array[i].equals(array[i-1])){
180
+                arrayAsString.append(array[i]);
181
+            } else {
182
+                //if the element in the array does not equal the previous element,
183
+                //   add it to the string after a space
184
+                arrayAsString.append(" ");
185
+                arrayAsString.append(array[i]);
186
+            }
187
+        }
188
+        //convert the new string to a space split string array
189
+        String[] packConsecDupArray = arrayAsString.toString().split(" ");
190
+        return packConsecDupArray;
161 191
     }
162
-
163 192
 }

+ 3
- 3
package.bluej 查看文件

@@ -4,8 +4,8 @@ dependency1.to=StringArrayUtils
4 4
 dependency1.type=UsesDependency
5 5
 editor.fx.0.height=713
6 6
 editor.fx.0.width=800
7
-editor.fx.0.x=466
8
-editor.fx.0.y=35
7
+editor.fx.0.x=306
8
+editor.fx.0.y=23
9 9
 objectbench.height=101
10 10
 objectbench.width=461
11 11
 package.divider.horizontal=0.6
@@ -13,7 +13,7 @@ package.divider.vertical=0.8007380073800738
13 13
 package.editor.height=427
14 14
 package.editor.width=674
15 15
 package.editor.x=202
16
-package.editor.y=149
16
+package.editor.y=136
17 17
 package.frame.height=600
18 18
 package.frame.width=800
19 19
 package.numDependencies=1