shakila 8 anni fa
parent
commit
2cc1b219cd
2 ha cambiato i file con 83 aggiunte e 50 eliminazioni
  1. 77
    44
      StringArrayUtils.java
  2. 6
    6
      package.bluej

+ 77
- 44
StringArrayUtils.java Vedi File

3
 import java.util.Collections;
3
 import java.util.Collections;
4
 import java.util.List;
4
 import java.util.List;
5
 import java.util.*;
5
 import java.util.*;
6
+import java.lang.StringBuilder;
7
+import java.lang.String;
6
 /**
8
 /**
7
  * Created by leon on 1/29/18.
9
  * Created by leon on 1/29/18.
8
  */
10
  */
94
      * @return true if each letter in the alphabet has been used in the array
96
      * @return true if each letter in the alphabet has been used in the array
95
      */ // TODO
97
      */ // TODO
96
     public static boolean isPangramic(String[] array) {
98
     public static boolean isPangramic(String[] array) {
99
+
100
+        String alphabet = "abcdefghijklmnopqrstuvwxyz";
101
+        boolean pan = true;
97
         boolean check = false;
102
         boolean check = false;
98
-        //Scanner reader = new Scanner();
99
-        String string = new String();//reader.nextLine();
100
-        Set character = new HashSet();
101
-        Set set = new HashSet();
102
-        for (char c:string.toUpperCase().toCharArray()){
103
             
103
             
104
-            if (character.equals(c)){
105
-                set.add(c);}
104
+        for (int i = 0; i>26; i++){
105
+            
106
+            for(String string:array){
107
+                
108
+                if (string.contains(Character.toString(alphabet.charAt(i)))){    
109
+                    check = true;
110
+                }
111
+                
112
+                else{ check = false;
113
+                }
114
+            }
115
+            if (check == false){
116
+                pan = false;
117
+                break;
118
+            }
106
         }
119
         }
107
-        if (set.size() == 26){
108
-            check = true;}
109
-            else {
110
-                check = false;}
111
-        return check;
112
-        
120
+        return pan;
113
     }
121
     }
114
-
115
-    /**
116
-     * @param array array of String objects
117
-     * @param value value to check array for
118
-     * @return number of occurrences the specified `value` has occurred
119
-     */ // Done
120
-    public static int getNumberOfOccurrences(String[] array, String value) {
122
+        /**
123
+         * @param array array of String objects
124
+         * @param value value to check array for
125
+         * @return number of occurrences the specified `value` has occurred
126
+         */ // Done
127
+        public static int getNumberOfOccurrences(String[] array, String value) {
121
         int counter = 0;
128
         int counter = 0;
122
         for (int i = 0; i<array.length; i++){
129
         for (int i = 0; i<array.length; i++){
123
-            if (array[i] == array[i+1]){
124
-                counter=counter+1;}
125
-                
130
+            if (array[i] == value){
131
+                counter=counter+1;   
126
             }
132
             }
133
+
134
+        }
135
+
127
         return counter;
136
         return counter;
128
     }
137
     }
129
 
138
 
133
      * @return array with identical contents excluding values of `value`
142
      * @return array with identical contents excluding values of `value`
134
      */ // Done
143
      */ // Done
135
     public static String[] removeValue(String[] array, String valueToRemove) {
144
     public static String[] removeValue(String[] array, String valueToRemove) {
136
-        //sort array, create new array, compare i, add new i to new array.
137
-        Arrays.sort(array);
138
-        ArrayList fresh = new ArrayList();
139
-        for (int i=0;i<array.length;i++){
140
-            if (array[i] != valueToRemove){
141
-                fresh.add(array[i]);
145
+
146
+        String [] temp = new String[array.length-(getNumberOfOccurrences(array,valueToRemove))];
147
+
148
+        for(int i = 0 ,j = 0;  i<array.length; i++){
149
+            if(!array[i].equals(valueToRemove)){
150
+
151
+                temp[j] = array[i];
152
+                j++;
142
             }
153
             }
143
-            
144
         }
154
         }
145
-        String[] list = (String[])(fresh.toArray()); //needed to convert to a string array, not a string
146
-        return list;
147
-        
148
-        //String delete = value.replaceAll (Character.toString(charToRemove), "");
149
-       //return delete;  
150
-        //return list.toString();
155
+
156
+        return temp;
157
+    }
158
+
159
+    public static int duplicates(String[] array){
160
+        String dupes = new String();
161
+        int counter = 0;
162
+        for (int i=0; i<array.length-1;i++){
163
+            if (array[i] == array[i+1]){
164
+                counter++;
165
+                //dupes += (array[i]);
166
+            }
167
+        }
168
+        return counter;
151
     }
169
     }
152
 
170
 
153
     /**
171
     /**
155
      * @return array of Strings with consecutive duplicates removes
173
      * @return array of Strings with consecutive duplicates removes
156
      */ // TODO
174
      */ // TODO
157
     public static String[] removeConsecutiveDuplicates(String[] array) {
175
     public static String[] removeConsecutiveDuplicates(String[] array) {
158
-        Arrays.sort(array);
159
-        ArrayList fresh = new ArrayList();
160
-        for (int i=0; i<array.length; i++){
161
-         if (array[i] != array[i-1] && array[i] != array[i+1]) {
162
-            fresh.add(array[i]);
176
+        String [] temp = new String[array.length-duplicates(array)];
177
+        int j = 0;
178
+        for(int i = 0; i<array.length-1; i++){
179
+            if(array[i] != array[i+1]){
180
+                temp[j] = array[i];
181
+                j++;
163
             }
182
             }
164
         }
183
         }
165
-        String[] list = (String[])(fresh.toArray());
166
-        return list;
184
+        temp[temp.length-1] = array[array.length-1];
185
+        return temp;
167
     }
186
     }
168
 
187
 
169
     /**
188
     /**
171
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
190
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
172
      */ // TODO
191
      */ // TODO
173
     public static String[] packConsecutiveDuplicates(String[] array) {
192
     public static String[] packConsecutiveDuplicates(String[] array) {
174
-        return null;
193
+        String[] pack = new String[array.length-duplicates(array)];
194
+        int j = 0;
195
+        pack[0]=array[0];
196
+        for(int i=1; i<array.length; i++){
197
+
198
+            if (array[i] == array[i-1]){
199
+                pack[j] += array[i];
200
+            }
201
+            else{
202
+                j++;
203
+                pack[j]=array[i];
204
+            }
205
+        }
206
+
207
+        return pack;
175
     }
208
     }
176
 
209
 
177
 }
210
 }

+ 6
- 6
package.bluej Vedi File

2
 dependency1.from=StringArrayUtilsTest
2
 dependency1.from=StringArrayUtilsTest
3
 dependency1.to=StringArrayUtils
3
 dependency1.to=StringArrayUtils
4
 dependency1.type=UsesDependency
4
 dependency1.type=UsesDependency
5
-editor.fx.0.height=714
6
-editor.fx.0.width=930
7
-editor.fx.0.x=310
8
-editor.fx.0.y=23
5
+editor.fx.0.height=716
6
+editor.fx.0.width=800
7
+editor.fx.0.x=-186
8
+editor.fx.0.y=-1039
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
12
 package.divider.vertical=0.8007380073800738
12
 package.divider.vertical=0.8007380073800738
13
 package.editor.height=427
13
 package.editor.height=427
14
 package.editor.width=674
14
 package.editor.width=674
15
-package.editor.x=255
16
-package.editor.y=130
15
+package.editor.x=561
16
+package.editor.y=-828
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