|
|
@@ -1,15 +1,18 @@
|
|
1
|
|
-
|
|
2
|
1
|
|
|
3
|
2
|
/**
|
|
4
|
3
|
* Created by leon on 1/29/18.
|
|
5
|
4
|
*/
|
|
|
5
|
+import java.util.Arrays;
|
|
6
|
6
|
public class StringArrayUtils {
|
|
7
|
7
|
/**
|
|
8
|
8
|
* @param array array of String objects
|
|
9
|
9
|
* @return first element of specified array
|
|
10
|
10
|
*/ // TODO
|
|
11
|
11
|
public static String getFirstElement(String[] array) {
|
|
12
|
|
- return null;
|
|
|
12
|
+ String first = "";
|
|
|
13
|
+
|
|
|
14
|
+ first += array[0];
|
|
|
15
|
+ return first;
|
|
13
|
16
|
}
|
|
14
|
17
|
|
|
15
|
18
|
/**
|
|
|
@@ -17,7 +20,11 @@ public class StringArrayUtils {
|
|
17
|
20
|
* @return second element in specified array
|
|
18
|
21
|
*/
|
|
19
|
22
|
public static String getSecondElement(String[] array) {
|
|
20
|
|
- return null;
|
|
|
23
|
+ String second = "";
|
|
|
24
|
+
|
|
|
25
|
+ second += array[1];
|
|
|
26
|
+
|
|
|
27
|
+ return second;
|
|
21
|
28
|
}
|
|
22
|
29
|
|
|
23
|
30
|
/**
|
|
|
@@ -25,7 +32,10 @@ public class StringArrayUtils {
|
|
25
|
32
|
* @return last element in specified array
|
|
26
|
33
|
*/ // TODO
|
|
27
|
34
|
public static String getLastElement(String[] array) {
|
|
28
|
|
- return null;
|
|
|
35
|
+ String last = "";
|
|
|
36
|
+
|
|
|
37
|
+ last += array[array.length-1];
|
|
|
38
|
+ return last;
|
|
29
|
39
|
}
|
|
30
|
40
|
|
|
31
|
41
|
/**
|
|
|
@@ -33,7 +43,10 @@ public class StringArrayUtils {
|
|
33
|
43
|
* @return second to last element in specified array
|
|
34
|
44
|
*/ // TODO
|
|
35
|
45
|
public static String getSecondToLastElement(String[] array) {
|
|
36
|
|
- return null;
|
|
|
46
|
+ String secLast = "";
|
|
|
47
|
+
|
|
|
48
|
+ secLast += array[array.length-2];
|
|
|
49
|
+ return secLast;
|
|
37
|
50
|
}
|
|
38
|
51
|
|
|
39
|
52
|
/**
|
|
|
@@ -42,7 +55,17 @@ public class StringArrayUtils {
|
|
42
|
55
|
* @return true if the array contains the specified `value`
|
|
43
|
56
|
*/ // TODO
|
|
44
|
57
|
public static boolean contains(String[] array, String value) {
|
|
45
|
|
- return false;
|
|
|
58
|
+ boolean contain = false;
|
|
|
59
|
+
|
|
|
60
|
+ for(String s : array){
|
|
|
61
|
+ if(s==value){
|
|
|
62
|
+ contain = true;
|
|
|
63
|
+
|
|
|
64
|
+ }
|
|
|
65
|
+
|
|
|
66
|
+ }
|
|
|
67
|
+
|
|
|
68
|
+ return contain;
|
|
46
|
69
|
}
|
|
47
|
70
|
|
|
48
|
71
|
/**
|
|
|
@@ -50,7 +73,17 @@ public class StringArrayUtils {
|
|
50
|
73
|
* @return an array with identical contents in reverse order
|
|
51
|
74
|
*/ // TODO
|
|
52
|
75
|
public static String[] reverse(String[] array) {
|
|
53
|
|
- return null;
|
|
|
76
|
+
|
|
|
77
|
+ String reversal;
|
|
|
78
|
+
|
|
|
79
|
+ for(int i = 0; i<array.length/2; i++){
|
|
|
80
|
+ reversal = array[i];
|
|
|
81
|
+ array[i] = array[array.length - i -1];
|
|
|
82
|
+ array[array.length - i -1] = reversal;
|
|
|
83
|
+ }
|
|
|
84
|
+
|
|
|
85
|
+ return array;
|
|
|
86
|
+
|
|
54
|
87
|
}
|
|
55
|
88
|
|
|
56
|
89
|
/**
|
|
|
@@ -58,7 +91,18 @@ public class StringArrayUtils {
|
|
58
|
91
|
* @return true if the order of the array is the same backwards and forwards
|
|
59
|
92
|
*/ // TODO
|
|
60
|
93
|
public static boolean isPalindromic(String[] array) {
|
|
61
|
|
- return false;
|
|
|
94
|
+ boolean pal = false;
|
|
|
95
|
+
|
|
|
96
|
+ String reversal;
|
|
|
97
|
+
|
|
|
98
|
+ for(int i = 0; i<array.length/2; i++){
|
|
|
99
|
+
|
|
|
100
|
+ if(array[i] == array[array.length - i -1]){
|
|
|
101
|
+ pal = true;
|
|
|
102
|
+ }
|
|
|
103
|
+
|
|
|
104
|
+ }
|
|
|
105
|
+ return pal;
|
|
62
|
106
|
}
|
|
63
|
107
|
|
|
64
|
108
|
/**
|
|
|
@@ -66,7 +110,27 @@ public class StringArrayUtils {
|
|
66
|
110
|
* @return true if each letter in the alphabet has been used in the array
|
|
67
|
111
|
*/ // TODO
|
|
68
|
112
|
public static boolean isPangramic(String[] array) {
|
|
69
|
|
- return false;
|
|
|
113
|
+ boolean pan = false;
|
|
|
114
|
+
|
|
|
115
|
+ StringBuilder build = new StringBuilder();
|
|
|
116
|
+
|
|
|
117
|
+ for(String s : array){
|
|
|
118
|
+ build.append(s);
|
|
|
119
|
+ }
|
|
|
120
|
+
|
|
|
121
|
+ String ofArray = "";
|
|
|
122
|
+ ofArray = build.toString();
|
|
|
123
|
+ for(char i = 'A'; i <= 'Z'; i++){
|
|
|
124
|
+ if((ofArray.indexOf(i) < 0) && (ofArray.indexOf((char)(i+32)) < 0))
|
|
|
125
|
+ { // goes thru the ASCII list
|
|
|
126
|
+ pan = false;
|
|
|
127
|
+ }
|
|
|
128
|
+ else{
|
|
|
129
|
+ pan = true;
|
|
|
130
|
+ }
|
|
|
131
|
+ }
|
|
|
132
|
+ return pan;
|
|
|
133
|
+
|
|
70
|
134
|
}
|
|
71
|
135
|
|
|
72
|
136
|
/**
|
|
|
@@ -75,7 +139,16 @@ public class StringArrayUtils {
|
|
75
|
139
|
* @return number of occurrences the specified `value` has occurred
|
|
76
|
140
|
*/ // TODO
|
|
77
|
141
|
public static int getNumberOfOccurrences(String[] array, String value) {
|
|
78
|
|
- return 0;
|
|
|
142
|
+ int occurs = 0;
|
|
|
143
|
+
|
|
|
144
|
+ for(int i=0; i<array.length; i++){
|
|
|
145
|
+ if(value.equals(array[i])){
|
|
|
146
|
+ occurs += 1 ;
|
|
|
147
|
+ }
|
|
|
148
|
+
|
|
|
149
|
+ }
|
|
|
150
|
+
|
|
|
151
|
+ return occurs;
|
|
79
|
152
|
}
|
|
80
|
153
|
|
|
81
|
154
|
/**
|
|
|
@@ -84,7 +157,21 @@ public class StringArrayUtils {
|
|
84
|
157
|
* @return array with identical contents excluding values of `value`
|
|
85
|
158
|
*/ // TODO
|
|
86
|
159
|
public static String[] removeValue(String[] array, String valueToRemove) {
|
|
87
|
|
- return null;
|
|
|
160
|
+ int count = 0;
|
|
|
161
|
+ int occur = getNumberOfOccurrences(array, valueToRemove);
|
|
|
162
|
+
|
|
|
163
|
+ String[] exclude = new String[array.length - occur];
|
|
|
164
|
+ for(String i : array){
|
|
|
165
|
+ if(i != valueToRemove){
|
|
|
166
|
+
|
|
|
167
|
+ exclude[count] = i;
|
|
|
168
|
+ count++;
|
|
|
169
|
+
|
|
|
170
|
+ }
|
|
|
171
|
+
|
|
|
172
|
+ }
|
|
|
173
|
+
|
|
|
174
|
+ return exclude;
|
|
88
|
175
|
}
|
|
89
|
176
|
|
|
90
|
177
|
/**
|
|
|
@@ -92,16 +179,36 @@ public class StringArrayUtils {
|
|
92
|
179
|
* @return array of Strings with consecutive duplicates removes
|
|
93
|
180
|
*/ // TODO
|
|
94
|
181
|
public static String[] removeConsecutiveDuplicates(String[] array) {
|
|
95
|
|
- return null;
|
|
|
182
|
+ String[] excluded = new String[array.length];
|
|
|
183
|
+ int newArrIndex = 0;
|
|
|
184
|
+ excluded[newArrIndex] = array[0];
|
|
|
185
|
+
|
|
|
186
|
+ newArrIndex++;
|
|
|
187
|
+ for(int i = 1; i<array.length; i++){
|
|
|
188
|
+ if(!(excluded[newArrIndex-1]).equals(array[i])){
|
|
|
189
|
+ excluded[newArrIndex] = array[i];
|
|
|
190
|
+ newArrIndex++;
|
|
|
191
|
+
|
|
|
192
|
+ }
|
|
|
193
|
+ }
|
|
|
194
|
+
|
|
|
195
|
+ String[] newArr = new String[newArrIndex];
|
|
|
196
|
+ System.arraycopy(excluded, 0, newArr, 0, newArrIndex);
|
|
|
197
|
+
|
|
|
198
|
+ return newArr;
|
|
|
199
|
+
|
|
96
|
200
|
}
|
|
97
|
201
|
|
|
98
|
202
|
/**
|
|
99
|
203
|
* @param array array of chars
|
|
100
|
|
- * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
|
|
|
204
|
+ * @return array of Strings with each consecutive duplicate
|
|
|
205
|
+ * occurrence concatenated as a single string in an array of Strings
|
|
101
|
206
|
*/ // TODO
|
|
102
|
207
|
public static String[] packConsecutiveDuplicates(String[] array) {
|
|
|
208
|
+ String[] newArr = new String[array.length];
|
|
|
209
|
+
|
|
|
210
|
+
|
|
103
|
211
|
return null;
|
|
104
|
212
|
}
|
|
105
|
213
|
|
|
106
|
|
-
|
|
107
|
214
|
}
|