Browse Source

Strings again.

Jennifer Tinkler 6 years ago
parent
commit
347381f3ff
1 changed files with 160 additions and 38 deletions
  1. 160
    38
      src/main/java/com/zipcodewilmington/StringArrayUtils.java

+ 160
- 38
src/main/java/com/zipcodewilmington/StringArrayUtils.java View File

1
 package com.zipcodewilmington;
1
 package com.zipcodewilmington;
2
 
2
 
3
+import java.util.*;
4
+import java.util.ArrayList;
5
+import java.util.Arrays;
6
+import java.util.HashMap;
7
+import java.util.Map;
8
+
3
 /**
9
 /**
4
  * Created by leon on 1/29/18.
10
  * Created by leon on 1/29/18.
5
  */
11
  */
9
      * @return first element of specified array
15
      * @return first element of specified array
10
      */ // TODO
16
      */ // TODO
11
     public static String getFirstElement(String[] array) {
17
     public static String getFirstElement(String[] array) {
12
-        return null;
18
+        return array[0];
13
     }
19
     }
14
 
20
 
15
     /**
21
     /**
17
      * @return second element in specified array
23
      * @return second element in specified array
18
      */
24
      */
19
     public static String getSecondElement(String[] array) {
25
     public static String getSecondElement(String[] array) {
20
-        return null;
26
+        return array[1];
21
     }
27
     }
22
 
28
 
23
     /**
29
     /**
25
      * @return last element in specified array
31
      * @return last element in specified array
26
      */ // TODO
32
      */ // TODO
27
     public static String getLastElement(String[] array) {
33
     public static String getLastElement(String[] array) {
28
-        return null;
34
+        return array[array.length - 1];
29
     }
35
     }
30
 
36
 
31
     /**
37
     /**
33
      * @return second to last element in specified array
39
      * @return second to last element in specified array
34
      */ // TODO
40
      */ // TODO
35
     public static String getSecondToLastElement(String[] array) {
41
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
42
+        return array[array.length - 2];
37
     }
43
     }
38
 
44
 
39
     /**
45
     /**
42
      * @return true if the array contains the specified `value`
48
      * @return true if the array contains the specified `value`
43
      */ // TODO
49
      */ // TODO
44
     public static boolean contains(String[] array, String value) {
50
     public static boolean contains(String[] array, String value) {
51
+
52
+        for (int i = 0; i < array.length; i++) {
53
+            if (array[i].equals(value)) {
54
+
55
+                return true;
56
+            }
57
+        }
45
         return false;
58
         return false;
46
     }
59
     }
47
 
60
 
50
      * @return an array with identical contents in reverse order
63
      * @return an array with identical contents in reverse order
51
      */ // TODO
64
      */ // TODO
52
     public static String[] reverse(String[] array) {
65
     public static String[] reverse(String[] array) {
53
-        return null;
66
+        String[] reverseArray = new String[array.length];
67
+
68
+        int counter = 0;
69
+        for (int i = array.length - 1; i >= 0; i--) {
70
+
71
+            reverseArray[counter] = array[i];
72
+
73
+            counter++;
74
+        }
75
+        return reverseArray;
54
     }
76
     }
55
 
77
 
56
     /**
78
     /**
58
      * @return true if the order of the array is the same backwards and forwards
80
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
81
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
82
     public static boolean isPalindromic(String[] array) {
61
-        return false;
83
+
84
+        String[] reverseArray = new String[array.length];
85
+
86
+        int counter = 0;
87
+
88
+        for (int i = array.length - 1; i >= 0; i--) {
89
+
90
+            reverseArray[counter] = array[i];
91
+
92
+            counter++;
93
+        }
94
+        return Arrays.equals(array, reverseArray);
62
     }
95
     }
63
 
96
 
64
     /**
97
     /**
66
      * @return true if each letter in the alphabet has been used in the array
99
      * @return true if each letter in the alphabet has been used in the array
67
      */ // TODO
100
      */ // TODO
68
     public static boolean isPangramic(String[] array) {
101
     public static boolean isPangramic(String[] array) {
69
-        return false;
70
-    }
71
 
102
 
72
-    /**
73
-     * @param array array of String objects
74
-     * @param value value to check array for
75
-     * @return number of occurrences the specified `value` has occurred
76
-     */ // TODO
77
-    public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
79
-    }
103
+        String abcs = "abcdefghijklmnopqrstuvwxyz";
80
 
104
 
81
-    /**
82
-     * @param array         array of String objects
83
-     * @param valueToRemove value to remove from array
84
-     * @return array with identical contents excluding values of `value`
85
-     */ // TODO
86
-    public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
88
-    }
105
+        for (int i = 0; i < abcs.length(); i++) {
89
 
106
 
90
-    /**
91
-     * @param array array of chars
92
-     * @return array of Strings with consecutive duplicates removes
93
-     */ // TODO
94
-    public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
96
-    }
107
+            int falseCount = 0;
97
 
108
 
98
-    /**
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
101
-     */ // TODO
102
-    public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
109
+            for (int j = 0; j < array.length; j++) {
110
+
111
+                String lowerCase = array[j].toLowerCase();
112
+
113
+                if (lowerCase.indexOf(abcs.charAt(i)) == -1) {
114
+
115
+                    falseCount++;
116
+                }
117
+            }
118
+            if (falseCount == array.length) {
119
+
120
+                return false;
121
+
122
+
123
+
124
+            }
125
+        } return true;
104
     }
126
     }
127
+        /**
128
+         * @param array array of String objects
129
+         * @param value value to check array for
130
+         * @return number of occurrences the specified `value` has occurred
131
+         */ // TODO
132
+
133
+        //check array to find a value
134
+        //take value and count how many times it is in array
135
+
136
+        public static int getNumberOfOccurrences (String[]array, String value){
137
+
138
+            int counter = 0;
139
+
140
+            for ( int i = 0; i < array.length; i++) {
141
+
142
+                if (array[i].equals(value)) {
143
+
144
+                    counter++;
145
+
146
+                }
147
+            } return counter;
148
+        }
149
+
150
+        /**
151
+         * @param array         array of String objects
152
+         * @param valueToRemove value to remove from array
153
+         * @return array with identical contents excluding values of `value`
154
+         */ // TODO
155
+        public static String[] removeValue (String[]array, String valueToRemove){
156
+            //make array list
157
+            ArrayList<String> temp = new ArrayList<String>();
158
+
159
+            for (int i = 0; i < array.length; i++) {
160
+
161
+                if (!array[i].equals(valueToRemove)) {
162
+
163
+                    temp.add(array[i]);
164
+                }
165
+
166
+            }
167
+                String[] newArray = temp.toArray(new String[temp.size()]);
168
+
169
+
170
+
171
+            return newArray;
172
+
173
+
174
+        }
175
+
176
+        /**
177
+         * @param array array of chars
178
+         * @return array of Strings with consecutive duplicates removes
179
+         */ // TODO
180
+        public static String[] removeConsecutiveDuplicates (String[]array) {
181
+                StringBuilder builder = new StringBuilder();
182
+                builder.append(array[0] + " ");
183
+
184
+                for (int i = 1; i < array.length; i++) {
185
+
186
+                    if (array[i] != array[i - 1]) {
187
+
188
+                        builder.append(array [i] + " ");
189
+                    }
190
+                }
191
+
192
+             return builder.toString().split(" ");
193
+        }
194
+        /**
195
+         * @param array array of chars
196
+         * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
197
+         */ // TODO
198
+        public static String[] packConsecutiveDuplicates (String[]array){
199
+
200
+            ArrayList<String> temp = new ArrayList<String>();
201
+
202
+            temp.add(array[0]);
203
+            int  tempPosition =0;
204
+
205
+            for (int i = 1; i < array.length; i++) {
206
+
207
+                if (array[i].charAt(0) == temp.get(tempPosition).charAt(0)) {
208
+
209
+                    String str = temp.get(tempPosition);
210
+
211
+                    str = str.concat(array[i]);
212
+
213
+                    temp.set(tempPosition, str);
214
+
215
+                }else {
216
+                    tempPosition++;
217
+                    temp.add(tempPosition, array[i]);
218
+
219
+                }
220
+            }
221
+
222
+            String [] newArray = temp.toArray(new String[temp.size()]);
223
+
224
+            return newArray;
225
+
226
+        }
105
 
227
 
106
 
228
 
107
-}
229
+    }