Przeglądaj źródła

completed StringArrayUtils

Chaitali Patel 8 lat temu
rodzic
commit
84d3adb7d3
2 zmienionych plików z 126 dodań i 12 usunięć
  1. 12
    0
      pom.xml
  2. 114
    12
      src/main/java/com/zipcodewilmington/StringArrayUtils.java

+ 12
- 0
pom.xml Wyświetl plik

@@ -7,6 +7,18 @@
7 7
     <groupId>com.zipcodewilmington.labs</groupId>
8 8
     <artifactId>arrayutils</artifactId>
9 9
     <version>1.0-SNAPSHOT</version>
10
+    <build>
11
+        <plugins>
12
+            <plugin>
13
+                <groupId>org.apache.maven.plugins</groupId>
14
+                <artifactId>maven-compiler-plugin</artifactId>
15
+                <configuration>
16
+                    <source>6</source>
17
+                    <target>6</target>
18
+                </configuration>
19
+            </plugin>
20
+        </plugins>
21
+    </build>
10 22
     <dependencies>
11 23
         <dependency>
12 24
             <groupId>junit</groupId>

+ 114
- 12
src/main/java/com/zipcodewilmington/StringArrayUtils.java Wyświetl plik

@@ -1,5 +1,8 @@
1 1
 package com.zipcodewilmington;
2 2
 
3
+import java.util.ArrayList;
4
+import java.util.Arrays;
5
+
3 6
 /**
4 7
  * Created by leon on 1/29/18.
5 8
  */
@@ -9,7 +12,7 @@ public class StringArrayUtils {
9 12
      * @return first element of specified array
10 13
      */ // TODO
11 14
     public static String getFirstElement(String[] array) {
12
-        return null;
15
+        return array[0];
13 16
     }
14 17
 
15 18
     /**
@@ -17,7 +20,7 @@ public class StringArrayUtils {
17 20
      * @return second element in specified array
18 21
      */
19 22
     public static String getSecondElement(String[] array) {
20
-        return null;
23
+        return array[1];
21 24
     }
22 25
 
23 26
     /**
@@ -25,7 +28,8 @@ public class StringArrayUtils {
25 28
      * @return last element in specified array
26 29
      */ // TODO
27 30
     public static String getLastElement(String[] array) {
28
-        return null;
31
+        return array[array.length - 1];
32
+
29 33
     }
30 34
 
31 35
     /**
@@ -33,7 +37,7 @@ public class StringArrayUtils {
33 37
      * @return second to last element in specified array
34 38
      */ // TODO
35 39
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
40
+        return array[array.length - 2];
37 41
     }
38 42
 
39 43
     /**
@@ -42,6 +46,11 @@ public class StringArrayUtils {
42 46
      * @return true if the array contains the specified `value`
43 47
      */ // TODO
44 48
     public static boolean contains(String[] array, String value) {
49
+
50
+        for (String n : array) {
51
+            if (value == n)
52
+                return true;
53
+        }
45 54
         return false;
46 55
     }
47 56
 
@@ -50,15 +59,27 @@ public class StringArrayUtils {
50 59
      * @return an array with identical contents in reverse order
51 60
      */ // TODO
52 61
     public static String[] reverse(String[] array) {
53
-        return null;
62
+        String[] rev = new String[array.length];
63
+        int x = 0;
64
+        for (int i = array.length - 1; i >= 0; i--) {
65
+            rev[x] = array[i];
66
+            x++;
67
+        }
68
+        return rev;
54 69
     }
55 70
 
71
+
56 72
     /**
57 73
      * @param array array of String objects
58 74
      * @return true if the order of the array is the same backwards and forwards
59 75
      */ // TODO
60 76
     public static boolean isPalindromic(String[] array) {
61
-        return false;
77
+        for (int i = 0; i < array.length / 2; i++) {
78
+            if (array[i] != array[array.length - 1 - i]) {
79
+                return false;
80
+            }
81
+        }
82
+        return true;
62 83
     }
63 84
 
64 85
     /**
@@ -66,7 +87,21 @@ public class StringArrayUtils {
66 87
      * @return true if each letter in the alphabet has been used in the array
67 88
      */ // TODO
68 89
     public static boolean isPangramic(String[] array) {
69
-        return false;
90
+        boolean isAPanGramic = true;
91
+        String strLower = "";
92
+
93
+        for (String var : array) {
94
+            strLower = strLower.concat(var.toLowerCase());
95
+        }
96
+        //loop through ANSI lower case values a-z
97
+        // IF! contains - boolean set to false
98
+        for (int i = 97; i <= 122; i++) {
99
+            if (!strLower.contains(Character.toString((char) i))) {
100
+                isAPanGramic = false;
101
+            }
102
+        }
103
+        return isAPanGramic;
104
+
70 105
     }
71 106
 
72 107
     /**
@@ -75,7 +110,17 @@ public class StringArrayUtils {
75 110
      * @return number of occurrences the specified `value` has occurred
76 111
      */ // TODO
77 112
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
113
+        int count = 0;
114
+
115
+        for (int i = 0; i < array.length; i++) {
116
+            String currentInt = array[i];
117
+
118
+            if (value == array[i]) {
119
+                count++;
120
+            }
121
+        }
122
+
123
+        return count;
79 124
     }
80 125
 
81 126
     /**
@@ -84,15 +129,39 @@ public class StringArrayUtils {
84 129
      * @return array with identical contents excluding values of `value`
85 130
      */ // TODO
86 131
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
132
+        String[] str = new String[array.length];
133
+        int j = 0;
134
+
135
+        for (int i = 0; i < array.length; i++) {
136
+            if (!valueToRemove.equals(array[i])) {
137
+                str[j] = array[i];
138
+                j = j + 1;
139
+                System.out.println(Arrays.toString(str));
140
+
141
+            }
142
+        }
143
+        //create a new array of length j
144
+
145
+        return Arrays.copyOf(str, j);
88 146
     }
89 147
 
148
+
90 149
     /**
91 150
      * @param array array of chars
92 151
      * @return array of Strings with consecutive duplicates removes
93 152
      */ // TODO
94 153
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
154
+        ArrayList<String> dups = new ArrayList<String>();
155
+        for (int i = 0; i < array.length; i++) {
156
+            if (i + 1 < array.length && (array[i].equals(array[i + 1]))) {
157
+                continue;
158
+            } else {
159
+                dups.add(array[i]);
160
+            }
161
+        }
162
+        String[] name = dups.toArray(new String[dups.size()]);
163
+
164
+        return name;
96 165
     }
97 166
 
98 167
     /**
@@ -100,8 +169,41 @@ public class StringArrayUtils {
100 169
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101 170
      */ // TODO
102 171
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
172
+        //String[] dups = new String[array.length];
173
+        ArrayList<String> str = new ArrayList<String>();
174
+
175
+        int length = array.length;
176
+
177
+        StringBuffer sbTemp = new StringBuffer("");
178
+        boolean flag = false;
179
+        int i = 0;
180
+        while (i < length - 1) {
181
+            if (array[i].equalsIgnoreCase(array[i + 1])) {
182
+                flag = true;
183
+                sbTemp.append(array[i]);
184
+            } else {
185
+                if (flag == true) {
186
+                    sbTemp.append(array[i]);
187
+                    str.add(sbTemp.toString());
188
+                    sbTemp = new StringBuffer("");
189
+                } else {
190
+                    str.add(array[i]);
191
+                }
192
+                flag = false;
193
+            }
194
+                i++;
195
+            }
196
+            if (flag == true) {
197
+                sbTemp.append(array[i]);
198
+                str.add(sbTemp.toString());
199
+                sbTemp = new StringBuffer("");
200
+            } else {
201
+                str.add(array[i]);
202
+            }
203
+            String[] dups = str.toArray(new String[str.size()]);
204
+            return dups;
205
+        }
206
+
104 207
     }
105 208
 
106 209
 
107
-}