|
@@ -1,4 +1,4 @@
|
1
|
|
-
|
|
1
|
+import java.util.Arrays;
|
2
|
2
|
|
3
|
3
|
/**
|
4
|
4
|
* Created by leon on 1/29/18.
|
|
@@ -9,7 +9,8 @@ public class StringArrayUtils {
|
9
|
9
|
* @return first element of specified array
|
10
|
10
|
*/ // TODO
|
11
|
11
|
public static String getFirstElement(String[] array) {
|
12
|
|
- return null;
|
|
12
|
+ return array[0];
|
|
13
|
+
|
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,7 +43,12 @@ 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
|
+ boolean answer = false;
|
|
47
|
+ for (String string : array) {
|
|
48
|
+ if (string.equalsIgnoreCase(value));
|
|
49
|
+ answer = true;
|
|
50
|
+ }
|
|
51
|
+ return answer;
|
46
|
52
|
}
|
47
|
53
|
|
48
|
54
|
/**
|
|
@@ -50,7 +56,12 @@ public class StringArrayUtils {
|
50
|
56
|
* @return an array with identical contents in reverse order
|
51
|
57
|
*/ // TODO
|
52
|
58
|
public static String[] reverse(String[] array) {
|
53
|
|
- return null;
|
|
59
|
+ String[] answer = new String[array.length];
|
|
60
|
+
|
|
61
|
+ for (int i = 0; i< array.length; i++)
|
|
62
|
+ answer[i] = array[array.length-1-i];
|
|
63
|
+
|
|
64
|
+ return answer;
|
54
|
65
|
}
|
55
|
66
|
|
56
|
67
|
/**
|
|
@@ -58,7 +69,12 @@ public class StringArrayUtils {
|
58
|
69
|
* @return true if the order of the array is the same backwards and forwards
|
59
|
70
|
*/ // TODO
|
60
|
71
|
public static boolean isPalindromic(String[] array) {
|
61
|
|
- return false;
|
|
72
|
+ boolean answer = false;
|
|
73
|
+ for (int i = 0; i < array.length; i++)
|
|
74
|
+ if (Arrays.equals(array, reverse(array))){
|
|
75
|
+ answer = true;
|
|
76
|
+ }
|
|
77
|
+ return answer;
|
62
|
78
|
}
|
63
|
79
|
|
64
|
80
|
/**
|
|
@@ -66,7 +82,17 @@ public class StringArrayUtils {
|
66
|
82
|
* @return true if each letter in the alphabet has been used in the array
|
67
|
83
|
*/ // TODO
|
68
|
84
|
public static boolean isPangramic(String[] array) {
|
69
|
|
- return false;
|
|
85
|
+
|
|
86
|
+ boolean isPangramic = false;
|
|
87
|
+ for (char i = 'A' ; i <= 'Z'; i++)
|
|
88
|
+ if (Arrays.toString(array).indexOf(i) < 0 && (Arrays.toString(array).indexOf((char) i +32) < 0)) {
|
|
89
|
+ isPangramic = false;
|
|
90
|
+ break;
|
|
91
|
+ } else {
|
|
92
|
+ isPangramic = true;
|
|
93
|
+ }
|
|
94
|
+
|
|
95
|
+ return isPangramic;
|
70
|
96
|
}
|
71
|
97
|
|
72
|
98
|
/**
|
|
@@ -75,7 +101,13 @@ public class StringArrayUtils {
|
75
|
101
|
* @return number of occurrences the specified `value` has occurred
|
76
|
102
|
*/ // TODO
|
77
|
103
|
public static int getNumberOfOccurrences(String[] array, String value) {
|
78
|
|
- return 0;
|
|
104
|
+ int count = 0;
|
|
105
|
+ for (String s: array) {
|
|
106
|
+ if (value.equals(s)) {
|
|
107
|
+ count ++;
|
|
108
|
+ }
|
|
109
|
+ }
|
|
110
|
+ return count;
|
79
|
111
|
}
|
80
|
112
|
|
81
|
113
|
/**
|
|
@@ -84,7 +116,16 @@ public class StringArrayUtils {
|
84
|
116
|
* @return array with identical contents excluding values of `value`
|
85
|
117
|
*/ // TODO
|
86
|
118
|
public static String[] removeValue(String[] array, String valueToRemove) {
|
87
|
|
- return null;
|
|
119
|
+ int index = 0;
|
|
120
|
+ int count = getNumberOfOccurrences(array, valueToRemove);
|
|
121
|
+ String[] newArray = new String[array.length-count];
|
|
122
|
+ for (int i = 0; i < array.length; i++) {
|
|
123
|
+ if (array[i] != valueToRemove) {
|
|
124
|
+ newArray[index] = array[i];
|
|
125
|
+ index++;
|
|
126
|
+ }
|
|
127
|
+ }
|
|
128
|
+ return newArray;
|
88
|
129
|
}
|
89
|
130
|
|
90
|
131
|
/**
|
|
@@ -92,6 +133,7 @@ public class StringArrayUtils {
|
92
|
133
|
* @return array of Strings with consecutive duplicates removes
|
93
|
134
|
*/ // TODO
|
94
|
135
|
public static String[] removeConsecutiveDuplicates(String[] array) {
|
|
136
|
+
|
95
|
137
|
return null;
|
96
|
138
|
}
|
97
|
139
|
|
|
@@ -103,5 +145,4 @@ public class StringArrayUtils {
|
103
|
145
|
return null;
|
104
|
146
|
}
|
105
|
147
|
|
106
|
|
-
|
107
|
148
|
}
|