Eric Foster 8 лет назад
Родитель
Сommit
eedd1c31ba
2 измененных файлов: 41 добавлений и 12 удалений
  1. 38
    9
      StringArrayUtils.java
  2. 3
    3
      package.bluej

+ 38
- 9
StringArrayUtils.java Просмотреть файл

1
 import java.util.Arrays;
1
 import java.util.Arrays;
2
 import java.io.*;
2
 import java.io.*;
3
-import java.util.*; 
3
+import java.util.*;
4
+import java.lang.StringBuilder;
4
 
5
 
5
 /**
6
 /**
6
  * Created by leon on 1/29/18.
7
  * Created by leon on 1/29/18.
84
      */ // TODO
85
      */ // TODO
85
     public static boolean isPangramic(String[] array) {
86
     public static boolean isPangramic(String[] array) {
86
         String arrayString = Arrays.toString(array).toLowerCase();
87
         String arrayString = Arrays.toString(array).toLowerCase();
87
-        
88
+
88
         if(arrayString.length() < 26){
89
         if(arrayString.length() < 26){
89
             return false;
90
             return false;
90
         }
91
         }
94
                 return false;
95
                 return false;
95
             }
96
             }
96
         }
97
         }
97
-        
98
+
98
         return true;
99
         return true;
99
     }
100
     }
100
 
101
 
111
                 numberOfOccurrences += 1;
112
                 numberOfOccurrences += 1;
112
             }
113
             }
113
         }
114
         }
114
-        
115
+
115
         return numberOfOccurrences;
116
         return numberOfOccurrences;
116
     }
117
     }
117
 
118
 
123
     public static String[] removeValue(String[] array, String valueToRemove) {
124
     public static String[] removeValue(String[] array, String valueToRemove) {
124
         int numberOfOccurrences = getNumberOfOccurrences(array, valueToRemove);
125
         int numberOfOccurrences = getNumberOfOccurrences(array, valueToRemove);
125
         String[] newArray = new String[array.length - numberOfOccurrences];
126
         String[] newArray = new String[array.length - numberOfOccurrences];
126
-        
127
+
127
         int i = 0;
128
         int i = 0;
128
         for(String element : array){
129
         for(String element : array){
129
             if(!element.equals(valueToRemove)){
130
             if(!element.equals(valueToRemove)){
133
         }
134
         }
134
         return newArray;
135
         return newArray;
135
     }
136
     }
136
-    
137
+
137
     public static int numberOfConsecutiveDuplicates(String[] array) {
138
     public static int numberOfConsecutiveDuplicates(String[] array) {
138
         int numberOfConsecDups = 0;
139
         int numberOfConsecDups = 0;
139
         for(int i=1; i<=array.length-1; i++){
140
         for(int i=1; i<=array.length-1; i++){
149
      * @return array of Strings with consecutive duplicates removes
150
      * @return array of Strings with consecutive duplicates removes
150
      */ // TODO
151
      */ // TODO
151
     public static String[] removeConsecutiveDuplicates(String[] array) {
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
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
168
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
158
      */ // TODO
169
      */ // TODO
159
     public static String[] packConsecutiveDuplicates(String[] array) {
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
 dependency1.type=UsesDependency
4
 dependency1.type=UsesDependency
5
 editor.fx.0.height=713
5
 editor.fx.0.height=713
6
 editor.fx.0.width=800
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
 objectbench.height=101
9
 objectbench.height=101
10
 objectbench.width=461
10
 objectbench.width=461
11
 package.divider.horizontal=0.6
11
 package.divider.horizontal=0.6
13
 package.editor.height=427
13
 package.editor.height=427
14
 package.editor.width=674
14
 package.editor.width=674
15
 package.editor.x=202
15
 package.editor.x=202
16
-package.editor.y=149
16
+package.editor.y=136
17
 package.frame.height=600
17
 package.frame.height=600
18
 package.frame.width=800
18
 package.frame.width=800
19
 package.numDependencies=1
19
 package.numDependencies=1