浏览代码

Working on methods

Trinh Tong 8 年前
父节点
当前提交
ccfb5c3892
共有 1 个文件被更改,包括 96 次插入11 次删除
  1. 96
    11
      src/main/java/com/zipcodewilmington/StringArrayUtils.java

+ 96
- 11
src/main/java/com/zipcodewilmington/StringArrayUtils.java 查看文件

@@ -1,5 +1,12 @@
1 1
 package com.zipcodewilmington;
2 2
 
3
+import com.sun.deploy.util.ArrayUtil;
4
+
5
+import java.lang.reflect.Array;
6
+import java.util.ArrayList;
7
+import java.util.Arrays;
8
+import java.util.Collections;
9
+
3 10
 /**
4 11
  * Created by leon on 1/29/18.
5 12
  */
@@ -9,7 +16,8 @@ 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
+        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,8 @@ 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
+
38
+        return array[array.length - 1];
29 39
     }
30 40
 
31 41
     /**
@@ -33,7 +43,8 @@ public class StringArrayUtils {
33 43
      * @return second to last element in specified array
34 44
      */ // TODO
35 45
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
46
+
47
+        return array[array.length - 2];
37 48
     }
38 49
 
39 50
     /**
@@ -42,7 +53,16 @@ public class StringArrayUtils {
42 53
      * @return true if the array contains the specified `value`
43 54
      */ // TODO
44 55
     public static boolean contains(String[] array, String value) {
45
-        return false;
56
+        boolean checker = false;
57
+
58
+        for (String word: array) {
59
+            if (word.equals(value)) {
60
+                checker = true;
61
+                break;
62
+            }
63
+        }
64
+
65
+        return checker;
46 66
     }
47 67
 
48 68
     /**
@@ -50,7 +70,13 @@ public class StringArrayUtils {
50 70
      * @return an array with identical contents in reverse order
51 71
      */ // TODO
52 72
     public static String[] reverse(String[] array) {
53
-        return null;
73
+
74
+        String[] reversedString = new String[array.length];
75
+
76
+        for (int i = array.length - 1, j = 0; i >= 0; i--, j++) {
77
+            reversedString[j] = array[i];
78
+        }
79
+        return reversedString;
54 80
     }
55 81
 
56 82
     /**
@@ -58,7 +84,15 @@ public class StringArrayUtils {
58 84
      * @return true if the order of the array is the same backwards and forwards
59 85
      */ // TODO
60 86
     public static boolean isPalindromic(String[] array) {
61
-        return false;
87
+        boolean palindromToken = false;
88
+        String[] reversedArray = StringArrayUtils.reverse(array);
89
+
90
+        for (int i = 0; i < array.length; i++) {
91
+            if (reversedArray[i].equals(array[i]) == true) {
92
+                palindromToken = true;
93
+            }
94
+        }
95
+        return palindromToken;
62 96
     }
63 97
 
64 98
     /**
@@ -66,7 +100,24 @@ public class StringArrayUtils {
66 100
      * @return true if each letter in the alphabet has been used in the array
67 101
      */ // TODO
68 102
     public static boolean isPangramic(String[] array) {
69
-        return false;
103
+
104
+        boolean finderToken = false;
105
+
106
+        StringBuilder stringMaker = new StringBuilder();
107
+        for (String word: array) {
108
+            stringMaker.append(word);
109
+        }
110
+
111
+        String fullArrayString = stringMaker.toString().toLowerCase();
112
+
113
+        for (char find = 'a'; find <= 'z'; find++) {
114
+            if (fullArrayString.indexOf(find) == -1) {
115
+                finderToken = false;
116
+            } else {
117
+                finderToken = true;
118
+            }
119
+        }
120
+        return finderToken;
70 121
     }
71 122
 
72 123
     /**
@@ -75,7 +126,13 @@ public class StringArrayUtils {
75 126
      * @return number of occurrences the specified `value` has occurred
76 127
      */ // TODO
77 128
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
129
+        int counter = 0;
130
+        for (String word: array) {
131
+            if (word.equals(value) == true) {
132
+                counter += 1;
133
+            }
134
+        }
135
+        return counter;
79 136
     }
80 137
 
81 138
     /**
@@ -84,7 +141,20 @@ public class StringArrayUtils {
84 141
      * @return array with identical contents excluding values of `value`
85 142
      */ // TODO
86 143
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
144
+
145
+        ArrayList<String> removedWordsArrayList = new ArrayList<String>(Arrays.asList(array));
146
+
147
+        for (int i = 0; i < removedWordsArrayList.size(); i++) {
148
+            if (removedWordsArrayList.get(i) == valueToRemove ) {
149
+                removedWordsArrayList.remove(i);
150
+            }
151
+        }
152
+
153
+        String[] newArray = new String[removedWordsArrayList.size()];
154
+
155
+        newArray = removedWordsArrayList.toArray(newArray);
156
+
157
+        return newArray;
88 158
     }
89 159
 
90 160
     /**
@@ -92,7 +162,19 @@ public class StringArrayUtils {
92 162
      * @return array of Strings with consecutive duplicates removes
93 163
      */ // TODO
94 164
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
165
+
166
+        String[] newArray = new String[array.length];
167
+        String compare = "";
168
+        int newLength = 0;
169
+
170
+        for (int i = 0; i < array.length; i++) {
171
+            if (array[i].equals(compare) == false) {
172
+                compare = array[i];
173
+                newArray[newLength] = compare;
174
+                newLength++;
175
+            }
176
+        }
177
+        return Arrays.copyOf(newArray, newLength);
96 178
     }
97 179
 
98 180
     /**
@@ -100,6 +182,9 @@ public class StringArrayUtils {
100 182
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101 183
      */ // TODO
102 184
     public static String[] packConsecutiveDuplicates(String[] array) {
185
+
186
+        // First a loop to check for duplicates
187
+        
103 188
         return null;
104 189
     }
105 190