|
@@ -1,15 +1,23 @@
|
1
|
1
|
package com.zipcodewilmington;
|
2
|
2
|
|
|
3
|
+import javax.print.DocFlavor;
|
|
4
|
+import java.lang.reflect.Array;
|
|
5
|
+import java.util.ArrayList;
|
|
6
|
+import java.util.Arrays;
|
|
7
|
+import java.util.Collections;
|
|
8
|
+import java.util.List;
|
|
9
|
+
|
3
|
10
|
/**
|
4
|
11
|
* Created by leon on 1/29/18.
|
5
|
12
|
*/
|
6
|
|
-public class StringArrayUtils {
|
|
13
|
+public class StringArrayUtils<start, end> {
|
7
|
14
|
/**
|
8
|
15
|
* @param array array of String objects
|
9
|
16
|
* @return first element of specified array
|
10
|
17
|
*/ // TODO
|
11
|
18
|
public static String getFirstElement(String[] array) {
|
12
|
|
- return null;
|
|
19
|
+
|
|
20
|
+ return array[0];
|
13
|
21
|
}
|
14
|
22
|
|
15
|
23
|
/**
|
|
@@ -17,7 +25,8 @@ public class StringArrayUtils {
|
17
|
25
|
* @return second element in specified array
|
18
|
26
|
*/
|
19
|
27
|
public static String getSecondElement(String[] array) {
|
20
|
|
- return null;
|
|
28
|
+
|
|
29
|
+ return array[1];
|
21
|
30
|
}
|
22
|
31
|
|
23
|
32
|
/**
|
|
@@ -25,7 +34,7 @@ public class StringArrayUtils {
|
25
|
34
|
* @return last element in specified array
|
26
|
35
|
*/ // TODO
|
27
|
36
|
public static String getLastElement(String[] array) {
|
28
|
|
- return null;
|
|
37
|
+ return array[array.length - 1];
|
29
|
38
|
}
|
30
|
39
|
|
31
|
40
|
/**
|
|
@@ -33,7 +42,7 @@ public class StringArrayUtils {
|
33
|
42
|
* @return second to last element in specified array
|
34
|
43
|
*/ // TODO
|
35
|
44
|
public static String getSecondToLastElement(String[] array) {
|
36
|
|
- return null;
|
|
45
|
+ return array[array.length - 2];
|
37
|
46
|
}
|
38
|
47
|
|
39
|
48
|
/**
|
|
@@ -42,15 +51,27 @@ public class StringArrayUtils {
|
42
|
51
|
* @return true if the array contains the specified `value`
|
43
|
52
|
*/ // TODO
|
44
|
53
|
public static boolean contains(String[] array, String value) {
|
45
|
|
- return false;
|
|
54
|
+ boolean str = false;
|
|
55
|
+ for (int x = 0; x < array.length; x++) {
|
|
56
|
+ if (value.equals(array[x])) {
|
|
57
|
+ str = true;
|
|
58
|
+ }
|
|
59
|
+ } return str;
|
46
|
60
|
}
|
47
|
61
|
|
48
|
62
|
/**
|
49
|
63
|
* @param array of String objects
|
50
|
64
|
* @return an array with identical contents in reverse order
|
51
|
65
|
*/ // TODO
|
52
|
|
- public static String[] reverse(String[] array) {
|
53
|
|
- return null;
|
|
66
|
+ public static String[] reverse (String[] array) {
|
|
67
|
+
|
|
68
|
+ String[] str = new String[array.length];
|
|
69
|
+
|
|
70
|
+ // for (int i = 0; i < array.length; i++ ){
|
|
71
|
+ for (int i = (array.length - 1); i >= 0; i--) {
|
|
72
|
+ str[i] = array[array.length - (i + 1)];
|
|
73
|
+ }
|
|
74
|
+ return str;
|
54
|
75
|
}
|
55
|
76
|
|
56
|
77
|
/**
|
|
@@ -58,16 +79,38 @@ public class StringArrayUtils {
|
58
|
79
|
* @return true if the order of the array is the same backwards and forwards
|
59
|
80
|
*/ // TODO
|
60
|
81
|
public static boolean isPalindromic(String[] array) {
|
61
|
|
- return false;
|
|
82
|
+
|
|
83
|
+ for (int i = 0; i < array.length / 2; i++) {
|
|
84
|
+ if (array[i] != array[array.length - 1 - i]) {
|
|
85
|
+ return false;
|
|
86
|
+ }
|
|
87
|
+ }
|
|
88
|
+ return true;
|
62
|
89
|
}
|
63
|
90
|
|
64
|
|
- /**
|
65
|
|
- * @param array array of String objects
|
66
|
|
- * @return true if each letter in the alphabet has been used in the array
|
|
91
|
+
|
|
92
|
+ /** // if the control reaches here, means all the elements were same
|
|
93
|
+ * @parareturn true;m array array of String objects
|
|
94
|
+ * @retu}rn true if each letter in the alphabet has been used in the array
|
67
|
95
|
*/ // TODO
|
68
|
96
|
public static boolean isPangramic(String[] array) {
|
69
|
|
- return false;
|
70
|
|
- }
|
|
97
|
+
|
|
98
|
+ boolean isAPanGramic = true;
|
|
99
|
+ String strLower = "";
|
|
100
|
+
|
|
101
|
+ //Array - toLowerCase - Concat
|
|
102
|
+ for (String var : array) {
|
|
103
|
+ strLower = strLower.concat(var.toLowerCase());
|
|
104
|
+ }
|
|
105
|
+ //loop through ANSI lower case values a-z
|
|
106
|
+ // IF! contains - boolean set to false
|
|
107
|
+ for(int i = 97; i <= 122; i++){
|
|
108
|
+ if(!strLower.contains(Character.toString((char)i))){
|
|
109
|
+ isAPanGramic = false;
|
|
110
|
+ }
|
|
111
|
+ }
|
|
112
|
+ return isAPanGramic;
|
|
113
|
+ }
|
71
|
114
|
|
72
|
115
|
/**
|
73
|
116
|
* @param array array of String objects
|
|
@@ -75,33 +118,88 @@ public class StringArrayUtils {
|
75
|
118
|
* @return number of occurrences the specified `value` has occurred
|
76
|
119
|
*/ // TODO
|
77
|
120
|
public static int getNumberOfOccurrences(String[] array, String value) {
|
78
|
|
- return 0;
|
|
121
|
+ int count = 0;
|
|
122
|
+
|
|
123
|
+ for (String str: array){
|
|
124
|
+ if(value.equals(str) )
|
|
125
|
+ count++;
|
|
126
|
+ }
|
|
127
|
+ return count;
|
79
|
128
|
}
|
80
|
129
|
|
81
|
130
|
/**
|
82
|
|
- * @param array array of String objects
|
|
131
|
+ * @param array array of String objects
|
83
|
132
|
* @param valueToRemove value to remove from array
|
84
|
133
|
* @return array with identical contents excluding values of `value`
|
85
|
134
|
*/ // TODO
|
86
|
135
|
public static String[] removeValue(String[] array, String valueToRemove) {
|
87
|
|
- return null;
|
88
|
|
- }
|
89
|
136
|
|
|
137
|
+ List<String> list = new ArrayList<String>(Arrays.asList(array));
|
|
138
|
+
|
|
139
|
+ list.remove(valueToRemove);
|
|
140
|
+
|
|
141
|
+ array = list.toArray(new String[0]);
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+ return array;
|
|
145
|
+
|
|
146
|
+ }
|
90
|
147
|
/**
|
91
|
148
|
* @param array array of chars
|
92
|
149
|
* @return array of Strings with consecutive duplicates removes
|
93
|
150
|
*/ // TODO
|
94
|
151
|
public static String[] removeConsecutiveDuplicates(String[] array) {
|
95
|
|
- return null;
|
96
|
|
- }
|
97
|
152
|
|
|
153
|
+
|
|
154
|
+ ArrayList<String> rm = new ArrayList<String>();
|
|
155
|
+ rm.add(array[0]);
|
|
156
|
+ for (int i = 1; i < array.length; i++) {
|
|
157
|
+
|
|
158
|
+ if (!array[i - 1].equals(array[i])) {
|
|
159
|
+
|
|
160
|
+ rm.add(array[i]);
|
|
161
|
+ }
|
|
162
|
+ }
|
|
163
|
+
|
|
164
|
+ String[] newArray = new String[rm.size()];
|
|
165
|
+ newArray = rm.toArray(new String[rm.size()]);
|
|
166
|
+
|
|
167
|
+ return newArray;
|
|
168
|
+ }
|
|
169
|
+
|
98
|
170
|
/**
|
99
|
|
- * @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
|
|
171
|
+ * @par return newList;am array array of chars
|
|
172
|
+ * @ret}urn array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
|
101
|
173
|
*/ // TODO
|
102
|
174
|
public static String[] packConsecutiveDuplicates(String[] array) {
|
103
|
|
- return null;
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+ ArrayList<String> arrayList = new ArrayList<String>();
|
|
178
|
+ String stri = new String();
|
|
179
|
+
|
|
180
|
+ stri = array[0];
|
|
181
|
+
|
|
182
|
+ arrayList.add(array[0]);
|
|
183
|
+ for (int i = 1; i < array.length; i++) {
|
|
184
|
+
|
|
185
|
+ if (array[i] == stri) {
|
|
186
|
+
|
|
187
|
+ arrayList.set(arrayList.size() - 1, arrayList.get(arrayList.size() - 1) + array[i]);
|
|
188
|
+
|
|
189
|
+ } else {
|
|
190
|
+
|
|
191
|
+ arrayList.add(array[i]);
|
|
192
|
+
|
|
193
|
+ stri = array[i];
|
|
194
|
+
|
|
195
|
+ }
|
|
196
|
+
|
|
197
|
+ }
|
|
198
|
+ String[] newArray = new String[arrayList.size()];
|
|
199
|
+ newArray = arrayList.toArray(new String[arrayList.size()]);
|
|
200
|
+
|
|
201
|
+ return newArray;
|
104
|
202
|
}
|
105
|
203
|
|
106
|
204
|
|
107
|
|
-}
|
|
205
|
+ }
|