Jose Bedolla 8 лет назад
Родитель
Сommit
4251745d45
1 измененных файлов: 98 добавлений и 11 удалений
  1. 98
    11
      src/main/java/com/zipcodewilmington/StringArrayUtils.java

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

@@ -1,4 +1,6 @@
1 1
 package com.zipcodewilmington;
2
+import java.lang.reflect.Array;
3
+import java.util.Arrays;
2 4
 
3 5
 /**
4 6
  * Created by leon on 1/29/18.
@@ -9,7 +11,7 @@ public class StringArrayUtils {
9 11
      * @return first element of specified array
10 12
      */ // TODO
11 13
     public static String getFirstElement(String[] array) {
12
-        return null;
14
+        return array[0];
13 15
     }
14 16
 
15 17
     /**
@@ -17,15 +19,18 @@ public class StringArrayUtils {
17 19
      * @return second element in specified array
18 20
      */
19 21
     public static String getSecondElement(String[] array) {
20
-        return null;
22
+        return array[1];
21 23
     }
22 24
 
23 25
     /**
24 26
      * @param array array of String objects
25 27
      * @return last element in specified array
26 28
      */ // TODO
27
-    public static String getLastElement(String[] array) {
28
-        return null;
29
+    public static String getLastElement(String[] array)
30
+    {
31
+    String lastElement = array[array.length - 1];
32
+
33
+    return lastElement;
29 34
     }
30 35
 
31 36
     /**
@@ -33,7 +38,10 @@ public class StringArrayUtils {
33 38
      * @return second to last element in specified array
34 39
      */ // TODO
35 40
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
41
+
42
+        String secondToLast  = array[array.length - 2];
43
+
44
+        return secondToLast;
37 45
     }
38 46
 
39 47
     /**
@@ -42,6 +50,14 @@ public class StringArrayUtils {
42 50
      * @return true if the array contains the specified `value`
43 51
      */ // TODO
44 52
     public static boolean contains(String[] array, String value) {
53
+
54
+        for(String w: array)
55
+        {
56
+            if(w==value)
57
+            {
58
+                return true;
59
+            }
60
+        }
45 61
         return false;
46 62
     }
47 63
 
@@ -50,7 +66,13 @@ public class StringArrayUtils {
50 66
      * @return an array with identical contents in reverse order
51 67
      */ // TODO
52 68
     public static String[] reverse(String[] array) {
53
-        return null;
69
+
70
+        String[] test = new String[array.length];
71
+       for(int i=array.length; i >0; i--)
72
+       {
73
+        test[i-1] = array[array.length-i];
74
+       }
75
+        return test;
54 76
     }
55 77
 
56 78
     /**
@@ -58,7 +80,18 @@ public class StringArrayUtils {
58 80
      * @return true if the order of the array is the same backwards and forwards
59 81
      */ // TODO
60 82
     public static boolean isPalindromic(String[] array) {
61
-        return false;
83
+
84
+        String[] reverse = new String[array.length];
85
+        for(int i=array.length; i>0; i--)
86
+        {
87
+            reverse[i-1] = array[array.length-1];
88
+        }
89
+        if(reverse.equals(array))
90
+        {
91
+            return false;
92
+        }
93
+
94
+        return true;
62 95
     }
63 96
 
64 97
     /**
@@ -66,7 +99,29 @@ public class StringArrayUtils {
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
+
103
+        String test = "abcdefghijklmnopqrstuvwxyz";
104
+        String keeper = "";
105
+        for(int i = 0; i<array.length; i++)
106
+        {
107
+            keeper+=array[i];//assign all values of array to string.
108
+        }
109
+
110
+        int measure = array.length;
111
+
112
+        for(int i=0; i< measure; i++)
113
+        {
114
+            char c = keeper.charAt(i);
115
+            String phrase = Character.toString(c).toLowerCase();
116
+            test = test.replaceAll(phrase,"");
117
+        }
118
+
119
+        if(test.equals(""))
120
+        {
121
+            return false;
122
+        }
123
+        return true;
124
+
70 125
     }
71 126
 
72 127
     /**
@@ -75,7 +130,16 @@ public class StringArrayUtils {
75 130
      * @return number of occurrences the specified `value` has occurred
76 131
      */ // TODO
77 132
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
133
+        int counter = 0;
134
+        for(int i=0; i<array.length; i++)
135
+        {
136
+            if(array[i].equals(value))
137
+            {
138
+                counter++;
139
+            }
140
+        }
141
+
142
+        return counter;
79 143
     }
80 144
 
81 145
     /**
@@ -84,7 +148,16 @@ public class StringArrayUtils {
84 148
      * @return array with identical contents excluding values of `value`
85 149
      */ // TODO
86 150
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
151
+        StringBuilder newValue = new StringBuilder();
152
+        for(int i=0; i<array.length; i++)
153
+        {
154
+            if(array[i]!=valueToRemove)
155
+            {
156
+                newValue.append(array[i] + " ");
157
+            }
158
+        }
159
+        return newValue.toString().split(" ");
160
+
88 161
     }
89 162
 
90 163
     /**
@@ -92,7 +165,18 @@ public class StringArrayUtils {
92 165
      * @return array of Strings with consecutive duplicates removes
93 166
      */ // TODO
94 167
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
168
+        //String[] compare = new String[array.length];
169
+        StringBuilder builder = new StringBuilder();
170
+        builder.append(array[0]+" ");
171
+        for(int i=1; i < array.length; i++)
172
+        {
173
+            if(!array[i].equals(array[i-1]))
174
+            {
175
+                builder.append(array[i]+" ");
176
+            }
177
+
178
+        }
179
+            return builder.toString().split(" ");
96 180
     }
97 181
 
98 182
     /**
@@ -100,6 +184,9 @@ public class StringArrayUtils {
100 184
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101 185
      */ // TODO
102 186
     public static String[] packConsecutiveDuplicates(String[] array) {
187
+
188
+        //cant figure it out
189
+
103 190
         return null;
104 191
     }
105 192