|
@@ -1,5 +1,10 @@
|
1
|
1
|
package com.zipcodewilmington;
|
2
|
2
|
|
|
3
|
+import com.sun.xml.internal.fastinfoset.util.StringArray;
|
|
4
|
+
|
|
5
|
+import java.util.ArrayList;
|
|
6
|
+import java.util.Arrays;
|
|
7
|
+
|
3
|
8
|
/**
|
4
|
9
|
* Created by leon on 1/29/18.
|
5
|
10
|
*/
|
|
@@ -9,7 +14,7 @@ public class StringArrayUtils {
|
9
|
14
|
* @return first element of specified array
|
10
|
15
|
*/ // TODO
|
11
|
16
|
public static String getFirstElement(String[] array) {
|
12
|
|
- return null;
|
|
17
|
+ return array[0];
|
13
|
18
|
}
|
14
|
19
|
|
15
|
20
|
/**
|
|
@@ -17,7 +22,7 @@ public class StringArrayUtils {
|
17
|
22
|
* @return second element in specified array
|
18
|
23
|
*/
|
19
|
24
|
public static String getSecondElement(String[] array) {
|
20
|
|
- return null;
|
|
25
|
+ return array[1];
|
21
|
26
|
}
|
22
|
27
|
|
23
|
28
|
/**
|
|
@@ -25,7 +30,7 @@ public class StringArrayUtils {
|
25
|
30
|
* @return last element in specified array
|
26
|
31
|
*/ // TODO
|
27
|
32
|
public static String getLastElement(String[] array) {
|
28
|
|
- return null;
|
|
33
|
+ return array[array.length - 1];
|
29
|
34
|
}
|
30
|
35
|
|
31
|
36
|
/**
|
|
@@ -33,7 +38,7 @@ public class StringArrayUtils {
|
33
|
38
|
* @return second to last element in specified array
|
34
|
39
|
*/ // TODO
|
35
|
40
|
public static String getSecondToLastElement(String[] array) {
|
36
|
|
- return null;
|
|
41
|
+ return array[array.length - 2];
|
37
|
42
|
}
|
38
|
43
|
|
39
|
44
|
/**
|
|
@@ -42,7 +47,13 @@ public class StringArrayUtils {
|
42
|
47
|
* @return true if the array contains the specified `value`
|
43
|
48
|
*/ // TODO
|
44
|
49
|
public static boolean contains(String[] array, String value) {
|
45
|
|
- return false;
|
|
50
|
+ boolean contains = false;
|
|
51
|
+ for (String s: array) {
|
|
52
|
+ if (s.equals(value)){
|
|
53
|
+ contains = true;
|
|
54
|
+ }
|
|
55
|
+ }
|
|
56
|
+ return contains;
|
46
|
57
|
}
|
47
|
58
|
|
48
|
59
|
/**
|
|
@@ -50,7 +61,14 @@ public class StringArrayUtils {
|
50
|
61
|
* @return an array with identical contents in reverse order
|
51
|
62
|
*/ // TODO
|
52
|
63
|
public static String[] reverse(String[] array) {
|
53
|
|
- return null;
|
|
64
|
+ String[] strArrReversed = new String[array.length];
|
|
65
|
+ int x = 0;
|
|
66
|
+ for (int i = array.length - 1; i >= 0; i--){
|
|
67
|
+ strArrReversed[x] = (array[i]);
|
|
68
|
+ x++;
|
|
69
|
+ }
|
|
70
|
+
|
|
71
|
+ return strArrReversed;
|
54
|
72
|
}
|
55
|
73
|
|
56
|
74
|
/**
|
|
@@ -58,7 +76,15 @@ public class StringArrayUtils {
|
58
|
76
|
* @return true if the order of the array is the same backwards and forwards
|
59
|
77
|
*/ // TODO
|
60
|
78
|
public static boolean isPalindromic(String[] array) {
|
61
|
|
- return false;
|
|
79
|
+ boolean isAPalindrom = true;
|
|
80
|
+ int countRev = array.length - 1;
|
|
81
|
+ for (int countUp = 0; countUp < array.length; countUp++){
|
|
82
|
+ if (array[countUp].equals(array[countRev]) == false){
|
|
83
|
+ isAPalindrom = false;
|
|
84
|
+ }
|
|
85
|
+ countRev--;
|
|
86
|
+ }
|
|
87
|
+ return isAPalindrom;
|
62
|
88
|
}
|
63
|
89
|
|
64
|
90
|
/**
|
|
@@ -66,7 +92,20 @@ public class StringArrayUtils {
|
66
|
92
|
* @return true if each letter in the alphabet has been used in the array
|
67
|
93
|
*/ // TODO
|
68
|
94
|
public static boolean isPangramic(String[] array) {
|
69
|
|
- return false;
|
|
95
|
+ boolean isAPanGramic = true;
|
|
96
|
+ String strLower = "";
|
|
97
|
+ //Array - toLowerCase - Concat
|
|
98
|
+ for (String var : array) {
|
|
99
|
+ strLower = strLower.concat(var.toLowerCase());
|
|
100
|
+ }
|
|
101
|
+ //loop through ANSI lower case values a-z
|
|
102
|
+ // IF! contains - boolean set to false
|
|
103
|
+ for(int i = 97; i <= 122; i++){
|
|
104
|
+ if(!strLower.contains(Character.toString((char)i))){
|
|
105
|
+ isAPanGramic = false;
|
|
106
|
+ }
|
|
107
|
+ }
|
|
108
|
+ return isAPanGramic;
|
70
|
109
|
}
|
71
|
110
|
|
72
|
111
|
/**
|
|
@@ -75,7 +114,13 @@ public class StringArrayUtils {
|
75
|
114
|
* @return number of occurrences the specified `value` has occurred
|
76
|
115
|
*/ // TODO
|
77
|
116
|
public static int getNumberOfOccurrences(String[] array, String value) {
|
78
|
|
- return 0;
|
|
117
|
+ int count = 0;
|
|
118
|
+ for (String arrayVar: array) {
|
|
119
|
+ if (arrayVar.equals(value)){
|
|
120
|
+ count++;
|
|
121
|
+ }
|
|
122
|
+ }
|
|
123
|
+ return count;
|
79
|
124
|
}
|
80
|
125
|
|
81
|
126
|
/**
|
|
@@ -84,7 +129,15 @@ public class StringArrayUtils {
|
84
|
129
|
* @return array with identical contents excluding values of `value`
|
85
|
130
|
*/ // TODO
|
86
|
131
|
public static String[] removeValue(String[] array, String valueToRemove) {
|
87
|
|
- return null;
|
|
132
|
+ ArrayList<String> arrayList = new ArrayList<String>();
|
|
133
|
+ for (int i = 0; i < array.length; i++) {
|
|
134
|
+ if(!array[i].equals(valueToRemove)) {
|
|
135
|
+ arrayList.add(array[i]);
|
|
136
|
+ }
|
|
137
|
+ }
|
|
138
|
+ String[] strReturn = arrayList.toArray(new String[arrayList.size()]);
|
|
139
|
+
|
|
140
|
+ return strReturn;
|
88
|
141
|
}
|
89
|
142
|
|
90
|
143
|
/**
|
|
@@ -92,7 +145,16 @@ public class StringArrayUtils {
|
92
|
145
|
* @return array of Strings with consecutive duplicates removes
|
93
|
146
|
*/ // TODO
|
94
|
147
|
public static String[] removeConsecutiveDuplicates(String[] array) {
|
95
|
|
- return null;
|
|
148
|
+ ArrayList<String> arrayList = new ArrayList<String>();
|
|
149
|
+ arrayList.add(array[0]);
|
|
150
|
+ for (int i = 0; i < array.length - 1; i++) {
|
|
151
|
+ if (!array[i].equals(array[i+1])) {
|
|
152
|
+ arrayList.add(array[i+1]);
|
|
153
|
+ }
|
|
154
|
+ }
|
|
155
|
+ String[] strReturn = arrayList.toArray(new String[arrayList.size()]);
|
|
156
|
+
|
|
157
|
+ return strReturn;
|
96
|
158
|
}
|
97
|
159
|
|
98
|
160
|
/**
|
|
@@ -100,7 +162,22 @@ public class StringArrayUtils {
|
100
|
162
|
* @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
|
101
|
163
|
*/ // TODO
|
102
|
164
|
public static String[] packConsecutiveDuplicates(String[] array) {
|
103
|
|
- return null;
|
|
165
|
+ ArrayList<String> arrayList = new ArrayList<String>();
|
|
166
|
+ arrayList.add(array[0]);
|
|
167
|
+ for(int i = 1; i < array.length; i++)
|
|
168
|
+ {
|
|
169
|
+ if(array[i].equals(array[i-1]))
|
|
170
|
+ {
|
|
171
|
+ String str = arrayList.get(arrayList.size() - 1).concat(array[i]);
|
|
172
|
+ arrayList.set(arrayList.size() - 1, str);
|
|
173
|
+ }
|
|
174
|
+ else
|
|
175
|
+ {
|
|
176
|
+ arrayList.add(array[i]);
|
|
177
|
+ }
|
|
178
|
+ }
|
|
179
|
+ String[] strReturn = arrayList.toArray(new String[arrayList.size()]);
|
|
180
|
+ return strReturn;
|
104
|
181
|
}
|
105
|
182
|
|
106
|
183
|
|