|
@@ -1,5 +1,12 @@
|
1
|
1
|
package com.zipcodewilmington;
|
2
|
2
|
|
|
3
|
+import com.sun.tools.javac.util.ArrayUtils;
|
|
4
|
+
|
|
5
|
+import java.sql.SQLOutput;
|
|
6
|
+import java.util.ArrayList;
|
|
7
|
+import java.util.Arrays;
|
|
8
|
+import java.util.List;
|
|
9
|
+
|
3
|
10
|
/**
|
4
|
11
|
* Created by leon on 1/29/18.
|
5
|
12
|
*/
|
|
@@ -9,7 +16,9 @@ public class StringArrayUtils {
|
9
|
16
|
* @return first element of specified array
|
10
|
17
|
*/ // TODO
|
11
|
18
|
public static String getFirstElement(String[] array) {
|
12
|
|
- return null;
|
|
19
|
+
|
|
20
|
+ String firstElement = array[0];
|
|
21
|
+ return firstElement;
|
13
|
22
|
}
|
14
|
23
|
|
15
|
24
|
/**
|
|
@@ -17,7 +26,9 @@ public class StringArrayUtils {
|
17
|
26
|
* @return second element in specified array
|
18
|
27
|
*/
|
19
|
28
|
public static String getSecondElement(String[] array) {
|
20
|
|
- return null;
|
|
29
|
+
|
|
30
|
+ String secondElement = array[1];
|
|
31
|
+ return secondElement;
|
21
|
32
|
}
|
22
|
33
|
|
23
|
34
|
/**
|
|
@@ -25,7 +36,9 @@ public class StringArrayUtils {
|
25
|
36
|
* @return last element in specified array
|
26
|
37
|
*/ // TODO
|
27
|
38
|
public static String getLastElement(String[] array) {
|
28
|
|
- return null;
|
|
39
|
+
|
|
40
|
+ String lastElement = array[array.length - 1];
|
|
41
|
+ return lastElement;
|
29
|
42
|
}
|
30
|
43
|
|
31
|
44
|
/**
|
|
@@ -33,7 +46,9 @@ public class StringArrayUtils {
|
33
|
46
|
* @return second to last element in specified array
|
34
|
47
|
*/ // TODO
|
35
|
48
|
public static String getSecondToLastElement(String[] array) {
|
36
|
|
- return null;
|
|
49
|
+
|
|
50
|
+ String secondToLastElement = array[array.length - 2];
|
|
51
|
+ return secondToLastElement;
|
37
|
52
|
}
|
38
|
53
|
|
39
|
54
|
/**
|
|
@@ -42,7 +57,8 @@ public class StringArrayUtils {
|
42
|
57
|
* @return true if the array contains the specified `value`
|
43
|
58
|
*/ // TODO
|
44
|
59
|
public static boolean contains(String[] array, String value) {
|
45
|
|
- return false;
|
|
60
|
+ Arrays.asList(array).contains(value);
|
|
61
|
+ return true;
|
46
|
62
|
}
|
47
|
63
|
|
48
|
64
|
/**
|
|
@@ -50,7 +66,16 @@ public class StringArrayUtils {
|
50
|
66
|
* @return an array with identical contents in reverse order
|
51
|
67
|
*/ // TODO
|
52
|
68
|
public static String[] reverse(String[] array) {
|
53
|
|
- return null;
|
|
69
|
+ String tmp;
|
|
70
|
+ for (int i = 0; i < array.length / 2; i++) {
|
|
71
|
+ tmp = array[i];
|
|
72
|
+ array[i] = array[array.length - 1 - i];
|
|
73
|
+ array[array.length - 1 - i] = tmp;
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ //System.out.println(array);
|
|
77
|
+
|
|
78
|
+ return array;
|
54
|
79
|
}
|
55
|
80
|
|
56
|
81
|
/**
|
|
@@ -58,7 +83,19 @@ public class StringArrayUtils {
|
58
|
83
|
* @return true if the order of the array is the same backwards and forwards
|
59
|
84
|
*/ // TODO
|
60
|
85
|
public static boolean isPalindromic(String[] array) {
|
61
|
|
- return false;
|
|
86
|
+ int i1 = 0;
|
|
87
|
+ int i2 = array.length - 1;
|
|
88
|
+
|
|
89
|
+ while (i2 > i1) {
|
|
90
|
+ if (array[i1] != array[i2]) {
|
|
91
|
+ return false;
|
|
92
|
+ }
|
|
93
|
+ ++i1;
|
|
94
|
+ --i2;
|
|
95
|
+ }
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+ return true;
|
62
|
99
|
}
|
63
|
100
|
|
64
|
101
|
/**
|
|
@@ -66,7 +103,15 @@ public class StringArrayUtils {
|
66
|
103
|
* @return true if each letter in the alphabet has been used in the array
|
67
|
104
|
*/ // TODO
|
68
|
105
|
public static boolean isPangramic(String[] array) {
|
69
|
|
- return false;
|
|
106
|
+ String str = String.join("", array).toLowerCase();
|
|
107
|
+ for (char i = 'a'; i <= 'z'; i++) {
|
|
108
|
+ if (str.contains(String.valueOf(i))) {
|
|
109
|
+ continue;
|
|
110
|
+ } else {
|
|
111
|
+ return false;
|
|
112
|
+ }
|
|
113
|
+ }
|
|
114
|
+ return true;
|
70
|
115
|
}
|
71
|
116
|
|
72
|
117
|
/**
|
|
@@ -75,24 +120,55 @@ public class StringArrayUtils {
|
75
|
120
|
* @return number of occurrences the specified `value` has occurred
|
76
|
121
|
*/ // TODO
|
77
|
122
|
public static int getNumberOfOccurrences(String[] array, String value) {
|
78
|
|
- return 0;
|
|
123
|
+ int counter = 0;
|
|
124
|
+ for (int i = 0; i < array.length; i++) {
|
|
125
|
+ if (array[i].contains(value)) {
|
|
126
|
+ counter++;
|
|
127
|
+ }
|
|
128
|
+ }
|
|
129
|
+ return counter;
|
79
|
130
|
}
|
80
|
131
|
|
|
132
|
+
|
81
|
133
|
/**
|
82
|
134
|
* @param array array of String objects
|
83
|
135
|
* @param valueToRemove value to remove from array
|
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
|
+ String[] newArr = new String[array.length - 1];
|
|
140
|
+ int counter = 0;
|
|
141
|
+ for (String i : array) {
|
|
142
|
+ if (!i.equals(valueToRemove)) {
|
|
143
|
+ newArr[counter++] = i;
|
|
144
|
+ }
|
|
145
|
+ }
|
|
146
|
+ return newArr;
|
88
|
147
|
}
|
89
|
148
|
|
|
149
|
+
|
90
|
150
|
/**
|
91
|
151
|
* @param array array of chars
|
92
|
152
|
* @return array of Strings with consecutive duplicates removes
|
93
|
153
|
*/ // TODO
|
94
|
154
|
public static String[] removeConsecutiveDuplicates(String[] array) {
|
95
|
|
- return null;
|
|
155
|
+ ArrayList<String> newArray = new ArrayList<>();
|
|
156
|
+
|
|
157
|
+ newArray.add(array[0]);
|
|
158
|
+ String last = array[0];
|
|
159
|
+
|
|
160
|
+ for (int i = 1; i < array.length; i++) {
|
|
161
|
+ String nextWord = array[i];
|
|
162
|
+ if (nextWord.equals(last)) {
|
|
163
|
+ newArray.set(newArray.size() - 1, newArray.get(newArray.size() - 1));
|
|
164
|
+ } else {
|
|
165
|
+ newArray.add(nextWord);
|
|
166
|
+ last = nextWord;
|
|
167
|
+ }
|
|
168
|
+ }
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+ return newArray.toArray(new String[newArray.size() - 1]);
|
96
|
172
|
}
|
97
|
173
|
|
98
|
174
|
/**
|
|
@@ -100,8 +176,24 @@ public class StringArrayUtils {
|
100
|
176
|
* @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
|
101
|
177
|
*/ // TODO
|
102
|
178
|
public static String[] packConsecutiveDuplicates(String[] array) {
|
103
|
|
- return null;
|
104
|
|
- }
|
105
|
179
|
|
|
180
|
+ ArrayList<String> newArray = new ArrayList<>();
|
106
|
181
|
|
|
182
|
+ newArray.add(array[0]);
|
|
183
|
+ String last = array[0];
|
|
184
|
+
|
|
185
|
+ for (int i = 1; i < array.length; i++) {
|
|
186
|
+ String nextWord = array[i];
|
|
187
|
+ if (nextWord.equals(last)) {
|
|
188
|
+ newArray.set(newArray.size() - 1, newArray.get(newArray.size() - 1) + nextWord);
|
|
189
|
+ } else {
|
|
190
|
+ newArray.add(nextWord);
|
|
191
|
+ last = nextWord;
|
|
192
|
+ }
|
|
193
|
+ }
|
|
194
|
+
|
|
195
|
+ return newArray.toArray(new String[newArray.size() - 1]);
|
|
196
|
+ }
|
107
|
197
|
}
|
|
198
|
+
|
|
199
|
+
|