Просмотр исходного кода

Still need to work on packConsecutiveDuplicates!

Kate Moore 8 лет назад
Родитель
Сommit
cc7e6c349e
1 измененных файлов: 105 добавлений и 12 удалений
  1. 105
    12
      src/main/java/com/zipcodewilmington/StringArrayUtils.java

+ 105
- 12
src/main/java/com/zipcodewilmington/StringArrayUtils.java Просмотреть файл

@@ -1,15 +1,22 @@
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
+
3 8
 /**
4 9
  * Created by leon on 1/29/18.
5 10
  */
6 11
 public class StringArrayUtils {
12
+
7 13
     /**
8 14
      * @param array array of String objects
9 15
      * @return first element of specified array
10 16
      */ // TODO
11 17
     public static String getFirstElement(String[] array) {
12
-        return null;
18
+
19
+        return array[0];
13 20
     }
14 21
 
15 22
     /**
@@ -17,7 +24,8 @@ public class StringArrayUtils {
17 24
      * @return second element in specified array
18 25
      */
19 26
     public static String getSecondElement(String[] array) {
20
-        return null;
27
+
28
+        return array[1];
21 29
     }
22 30
 
23 31
     /**
@@ -25,7 +33,8 @@ public class StringArrayUtils {
25 33
      * @return last element in specified array
26 34
      */ // TODO
27 35
     public static String getLastElement(String[] array) {
28
-        return null;
36
+
37
+        return array[array.length - 1];
29 38
     }
30 39
 
31 40
     /**
@@ -33,7 +42,8 @@ public class StringArrayUtils {
33 42
      * @return second to last element in specified array
34 43
      */ // TODO
35 44
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
45
+
46
+        return array[array.length - 2];
37 47
     }
38 48
 
39 49
     /**
@@ -42,6 +52,10 @@ public class StringArrayUtils {
42 52
      * @return true if the array contains the specified `value`
43 53
      */ // TODO
44 54
     public static boolean contains(String[] array, String value) {
55
+        for (String n : array) {
56
+            if (value == n)
57
+                return true;
58
+        }
45 59
         return false;
46 60
     }
47 61
 
@@ -50,7 +64,11 @@ public class StringArrayUtils {
50 64
      * @return an array with identical contents in reverse order
51 65
      */ // TODO
52 66
     public static String[] reverse(String[] array) {
53
-        return null;
67
+        String a[] = new String[array.length];
68
+        for (int i = 0; i < a.length; i++) {
69
+            a[i] = array[array.length - 1 - i];
70
+        }
71
+        return a;
54 72
     }
55 73
 
56 74
     /**
@@ -58,7 +76,12 @@ 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
+        for (int i = 0; i < array.length / 2; i++) {
80
+            if (array[i] != array[array.length - 1 - i])
81
+                return false;
82
+
83
+        }
84
+        return true;
62 85
     }
63 86
 
64 87
     /**
@@ -66,6 +89,23 @@ public class StringArrayUtils {
66 89
      * @return true if each letter in the alphabet has been used in the array
67 90
      */ // TODO
68 91
     public static boolean isPangramic(String[] array) {
92
+
93
+        String[] arr = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"};
94
+
95
+        int count = 0;
96
+
97
+        for (int i = 0; i < arr.length; i++) {
98
+            for (int j = 0; j < array.length; j++) {
99
+                if (array[j].contains(arr[i])) {
100
+                    count++;
101
+                }
102
+
103
+            }
104
+        }
105
+        if (count >= 26) {
106
+            return true;
107
+        }
108
+
69 109
         return false;
70 110
     }
71 111
 
@@ -75,7 +115,15 @@ public class StringArrayUtils {
75 115
      * @return number of occurrences the specified `value` has occurred
76 116
      */ // TODO
77 117
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
118
+        int count = 0;
119
+        for (String n : array) {
120
+            if (n.equals(value)) {
121
+                count += 1;
122
+            }
123
+        }
124
+
125
+        return count;
126
+
79 127
     }
80 128
 
81 129
     /**
@@ -84,7 +132,21 @@ public class StringArrayUtils {
84 132
      * @return array with identical contents excluding values of `value`
85 133
      */ // TODO
86 134
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
135
+
136
+        ArrayList<String> list = new ArrayList<String>();
137
+
138
+        for (int i = 0; i < array.length; i++) {
139
+            if (array[i] != (valueToRemove)) {
140
+                list.add(array[i]);
141
+            }
142
+
143
+        }
144
+        String[] newStr = new String[list.size()];
145
+        for (int i = 0; i < list.size(); i++) {
146
+            newStr[i] = list.get(i);
147
+        }
148
+
149
+        return newStr;
88 150
     }
89 151
 
90 152
     /**
@@ -92,7 +154,18 @@ public class StringArrayUtils {
92 154
      * @return array of Strings with consecutive duplicates removes
93 155
      */ // TODO
94 156
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
157
+
158
+        ArrayList<String> list = new ArrayList<String>();
159
+
160
+
161
+        for (int i = 0; i < array.length; i++) {
162
+            if (i + 1 < array.length && array[i].equals(array[i + 1])) {
163
+            } else {
164
+                list.add(array[i]);
165
+            }
166
+        }
167
+        String[] result = list.toArray(new String[list.size()]);
168
+        return result;
96 169
     }
97 170
 
98 171
     /**
@@ -100,8 +173,28 @@ public class StringArrayUtils {
100 173
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101 174
      */ // TODO
102 175
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
104
-    }
105 176
 
177
+       /* int countDups = 0;
178
+
179
+        for (int i = 0; i< array.length; i++) {
180
+            for(int j = i+1; j <array.length; j++) {
181
+                if(array[i].equals(array[j])) {
182
+                   countDups+=1;
183
+                }
184
+            }
185
+        }
186
+
187
+        String arr2[] = new String[array.length - countDups];
188
+
189
+        for (String element: array) {
190
+            if( element.equals(element)) {
106 191
 
107
-}
192
+            }
193
+        }
194
+
195
+        return arr2
196
+    }*/
197
+
198
+        return null;
199
+    }
200
+}