Joshua Chung vor 8 Jahren
Ursprung
Commit
2288989ed4
4 geänderte Dateien mit 172 neuen und 23 gelöschten Zeilen
  1. 0
    0
      README.md
  2. 86
    23
      StringArrayUtils.java
  3. 44
    0
      StringArrayUtilsTest.java
  4. 42
    0
      package.bluej

+ 0
- 0
README.md Datei anzeigen


+ 86
- 23
StringArrayUtils.java Datei anzeigen

1
-import java.util.Arrays;
1
+import java.util.*;
2
 
2
 
3
 /**
3
 /**
4
  * Created by leon on 1/29/18.
4
  * Created by leon on 1/29/18.
24
      * @param array array of String objects
24
      * @param array array of String objects
25
      * @return last element in specified array
25
      * @return last element in specified array
26
      */ // TODO
26
      */ // TODO
27
-    public static String getLastElement(String[] array) {        
28
-        return array[array.length - 1];
27
+    public static String getLastElement(String[] array) {
28
+        return array[array.length-1];
29
     }
29
     }
30
 
30
 
31
     /**
31
     /**
41
      * @param value value to check array for
41
      * @param value value to check array for
42
      * @return true if the array contains the specified `value`
42
      * @return true if the array contains the specified `value`
43
      */ // TODO
43
      */ // TODO
44
-    public static boolean contains(String[] array, String value) {        
44
+    public static boolean contains(String[] array, String value) {
45
         
45
         
46
         for(String x : array) {
46
         for(String x : array) {
47
-            if(value == x) {
47
+            if(value.equals(x)) {
48
                 return true;
48
                 return true;
49
             }
49
             }
50
         }
50
         }
51
         return false;
51
         return false;
52
     }
52
     }
53
 
53
 
54
+
54
     /**
55
     /**
55
      * @param array of String objects
56
      * @param array of String objects
56
      * @return an array with identical contents in reverse order
57
      * @return an array with identical contents in reverse order
59
         
60
         
60
         String[] reverseArr = new String[array.length];
61
         String[] reverseArr = new String[array.length];
61
         
62
         
62
-        for(int i=0; i<array.length; i++){            
63
+        for(int i = 0; i < array.length; i++){
63
             reverseArr[i] = array[array.length - i - 1];
64
             reverseArr[i] = array[array.length - i - 1];
64
         }
65
         }
65
         return reverseArr;
66
         return reverseArr;
74
         String[] reverseArr = reverse(array);
75
         String[] reverseArr = reverse(array);
75
         String[] regArr = new String[array.length];
76
         String[] regArr = new String[array.length];
76
         
77
         
77
-        for(int i=0; i<array.length; i++){
78
-             regArr[i] = array[i];
79
-        }
80
-        
81
-        if(regArr ==  reverseArr) {
82
-            return true;
83
-        }
78
+        for(int i= 0; i < array.length; i++){
79
+            regArr[i] = array[i];
80
+                if(regArr[i] == reverseArr[i]) {
81
+                    return true;
82
+                }
83
+            }
84
         return false;
84
         return false;
85
     }
85
     }
86
 
86
 
87
-
88
-
89
     /**
87
     /**
90
      * @param array array of String objects
88
      * @param array array of String objects
91
      * @return true if each letter in the alphabet has been used in the array
89
      * @return true if each letter in the alphabet has been used in the array
92
      */ // TODO
90
      */ // TODO
93
     public static boolean isPangramic(String[] array) {
91
     public static boolean isPangramic(String[] array) {
94
-        return false;
92
+        String alphabet = "abcdefghijklmnopqrstuvwxyz";
93
+        String fullCombinedString = String.join("", array).toLowerCase();
94
+        for(char c : alphabet.toCharArray()) {
95
+            if(fullCombinedString.indexOf(c) < 0) {
96
+                return false;
97
+            }
98
+        }
99
+        return true;
100
+        
101
+        
102
+        /*
103
+        for(char c : alphabet.toCharArray()) {
104
+            boolean found = false;
105
+            for(String word : array) {
106
+                word = word.toLowerCase();
107
+                if(word.indexOf(c) >= 0)
108
+                found = true;
109
+                break;
110
+            }
111
+            if(!found) {
112
+                return false;
113
+        }
114
+    }
115
+        return true;*/
95
     }
116
     }
96
 
117
 
118
+
97
     /**
119
     /**
98
      * @param array array of String objects
120
      * @param array array of String objects
99
      * @param value value to check array for
121
      * @param value value to check array for
100
      * @return number of occurrences the specified `value` has occurred
122
      * @return number of occurrences the specified `value` has occurred
101
      */ // TODO
123
      */ // TODO
102
     public static int getNumberOfOccurrences(String[] array, String value) {
124
     public static int getNumberOfOccurrences(String[] array, String value) {
103
-        return 0;
125
+        int count = 0;
126
+        for(String s : array) {
127
+            if(s == value) {
128
+                count++;
129
+            }
130
+        }
131
+        return count;
104
     }
132
     }
105
 
133
 
106
     /**
134
     /**
109
      * @return array with identical contents excluding values of `value`
137
      * @return array with identical contents excluding values of `value`
110
      */ // TODO
138
      */ // TODO
111
     public static String[] removeValue(String[] array, String valueToRemove) {
139
     public static String[] removeValue(String[] array, String valueToRemove) {
112
-        return null;
140
+        List<String> newArray = new ArrayList<>();
141
+        
142
+        for(String s : array) {
143
+            if(s != valueToRemove) {
144
+                newArray.add(s);
145
+            }
146
+        }
147
+        return newArray.toArray(new String[] {});
113
     }
148
     }
114
 
149
 
115
     /**
150
     /**
117
      * @return array of Strings with consecutive duplicates removes
152
      * @return array of Strings with consecutive duplicates removes
118
      */ // TODO
153
      */ // TODO
119
     public static String[] removeConsecutiveDuplicates(String[] array) {
154
     public static String[] removeConsecutiveDuplicates(String[] array) {
155
+        List<String> newArray = new ArrayList<>();
120
         
156
         
157
+        for(int i = 0; i < array.length; i++) {
158
+            String added = array[i];
159
+            newArray.add(added);
160
+            int toRemove = 0;
161
+            
162
+            for(; toRemove + i + 1 < array.length; toRemove++) {
163
+                if(array[i + 1 + toRemove] != added){
164
+                    break;
165
+                }                
166
+            }
167
+            i += toRemove;        
168
+        }
169
+        return newArray.toArray(new String[] {});
170
+}
121
 
171
 
122
-        
123
-        return null;    
124
-    }
125
-    
126
     /**
172
     /**
127
      * @param array array of chars
173
      * @param array array of chars
128
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
174
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
129
      */ // TODO
175
      */ // TODO
130
     public static String[] packConsecutiveDuplicates(String[] array) {
176
     public static String[] packConsecutiveDuplicates(String[] array) {
131
-        return null;
177
+        List<String> newArray = new ArrayList<>();
178
+        
179
+        for(int i = 0; i < array.length; i++) {
180
+            String added = array[i];
181
+            String toAdd = added;
182
+            int toRemove = 0;
183
+            
184
+            for(; toRemove + i + 1 < array.length; toRemove++) {
185
+                if(array[i + 1 + toRemove] != added){
186
+                    break;
187
+                }  
188
+                toAdd += array[i + 1 + toRemove];
189
+            }
190
+            newArray.add(toAdd);
191
+            i += toRemove;        
192
+        }
193
+        System.out.println(Arrays.toString(newArray.toArray()));
194
+        return newArray.toArray(new String[] {});
132
     }
195
     }
133
 
196
 
134
 
197
 

+ 44
- 0
StringArrayUtilsTest.java Datei anzeigen

33
         Assert.assertEquals(expected, actual);
33
         Assert.assertEquals(expected, actual);
34
     }
34
     }
35
 
35
 
36
+
37
+
38
+
39
+
40
+
41
+
42
+
43
+
44
+
45
+
46
+
47
+
48
+
36
     @Test
49
     @Test
37
     public void testGetSecondElement1() {
50
     public void testGetSecondElement1() {
38
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
51
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
49
         Assert.assertEquals(expected, actual);
62
         Assert.assertEquals(expected, actual);
50
     }
63
     }
51
 
64
 
65
+
52
     @Test
66
     @Test
53
     public void testGetSecondElement3() {
67
     public void testGetSecondElement3() {
54
         String[] array = {"brown", "fox", "jumps", "over", "the", "lazy", "dog"};
68
         String[] array = {"brown", "fox", "jumps", "over", "the", "lazy", "dog"};
57
         Assert.assertEquals(expected, actual);
71
         Assert.assertEquals(expected, actual);
58
     }
72
     }
59
 
73
 
74
+
75
+
76
+
77
+
78
+
79
+
80
+
81
+
82
+
83
+
60
     @Test
84
     @Test
61
     public void testGetLastElement1() {
85
     public void testGetLastElement1() {
62
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
86
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
82
         Assert.assertEquals(expected, actual);
106
         Assert.assertEquals(expected, actual);
83
     }
107
     }
84
 
108
 
109
+
110
+
111
+
112
+
113
+
114
+
115
+
116
+
117
+
118
+
119
+
120
+
121
+
122
+
123
+
124
+
125
+
126
+
127
+
128
+
85
     @Test
129
     @Test
86
     public void testGetSecondToLastElement1() {
130
     public void testGetSecondToLastElement1() {
87
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
131
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};

+ 42
- 0
package.bluej Datei anzeigen

1
+#BlueJ package file
2
+dependency1.from=StringArrayUtilsTest
3
+dependency1.to=StringArrayUtils
4
+dependency1.type=UsesDependency
5
+editor.fx.0.height=1087
6
+editor.fx.0.width=1239
7
+editor.fx.0.x=600
8
+editor.fx.0.y=0
9
+objectbench.height=155
10
+objectbench.width=760
11
+package.divider.horizontal=0.6
12
+package.divider.vertical=0.676
13
+package.editor.height=331
14
+package.editor.width=670
15
+package.editor.x=78
16
+package.editor.y=78
17
+package.frame.height=600
18
+package.frame.width=800
19
+package.numDependencies=1
20
+package.numTargets=2
21
+package.showExtends=true
22
+package.showUses=true
23
+project.charset=windows-1252
24
+readme.height=58
25
+readme.name=@README
26
+readme.width=47
27
+readme.x=10
28
+readme.y=10
29
+target1.height=50
30
+target1.name=StringArrayUtilsTest
31
+target1.showInterface=false
32
+target1.type=UnitTestTargetJunit4
33
+target1.width=150
34
+target1.x=120
35
+target1.y=10
36
+target2.height=50
37
+target2.name=StringArrayUtils
38
+target2.showInterface=false
39
+target2.type=ClassTarget
40
+target2.width=120
41
+target2.x=70
42
+target2.y=70