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