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