소스 검색

wednesday 6:50

Seth 8 년 전
부모
커밋
395d9a715f
2개의 변경된 파일53개의 추가작업 그리고 6개의 파일을 삭제
  1. 13
    0
      pom.xml
  2. 40
    6
      src/main/java/com/zipcodewilmington/StringArrayUtils.java

+ 13
- 0
pom.xml 파일 보기

@@ -14,6 +14,19 @@
14 14
             <version>4.12</version>
15 15
         </dependency>
16 16
     </dependencies>
17
+    <build>
18
+        <plugins>
19
+            <plugin>
20
+                <groupId>org.apache.maven.plugins</groupId>
21
+                <artifactId>maven-compiler-plugin</artifactId>
22
+                <configuration>
23
+                    <source>1.8</source>
24
+                    <target>1.8</target>
25
+                </configuration>
26
+            </plugin>
27
+        </plugins>
28
+    </build>
29
+
17 30
 
18 31
 
19 32
 </project>

+ 40
- 6
src/main/java/com/zipcodewilmington/StringArrayUtils.java 파일 보기

@@ -1,6 +1,8 @@
1 1
 package com.zipcodewilmington;
2 2
 
3 3
 
4
+import java.util.Arrays;
5
+
4 6
 /**
5 7
  * Created by leon on 1/29/18.
6 8
  */
@@ -124,13 +126,16 @@ public class StringArrayUtils {
124 126
 
125 127
         public static String[] removeValue(String[] array, String valueToRemove) {
126 128
             String[] tempArray = new String[array.length];
129
+            int newIdx = 0;
127 130
             for(int i = 0; i < array.length; i++){
128
-                if(array[i] == valueToRemove) {
129
-
131
+                if(array[i] != valueToRemove) {
132
+                    tempArray[newIdx] = array[i];
133
+                    newIdx++;
130 134
                 }
131 135
 
132 136
             }
133
-            return array;
137
+            return Arrays.copyOf(tempArray, newIdx);
138
+
134 139
         }
135 140
 
136 141
     /**
@@ -138,8 +143,20 @@ public class StringArrayUtils {
138 143
      * @return array of Strings with consecutive duplicates removes
139 144
      */ // TODO
140 145
     public static String[] removeConsecutiveDuplicates(String[] array) {
141
-        return null;
146
+        String[] tempArray = new String[array.length];
147
+        int newIdx = 0;
148
+
149
+        for(int i = 1; i < array.length; i++){
150
+            String current = array[i - 1];
151
+            if(!current.equals(array[i])){
152
+                tempArray[newIdx] = array[i - 1];
153
+              newIdx++;
154
+            }
142 155
         }
156
+        tempArray[newIdx] = array[array.length - 1];
157
+        return Arrays.copyOf(tempArray, newIdx + 1);
158
+
159
+    }
143 160
 
144 161
 
145 162
 
@@ -148,10 +165,27 @@ public class StringArrayUtils {
148 165
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
149 166
      */ // TODO
150 167
     public static String[] packConsecutiveDuplicates(String[] array) {
151
-        return null;
152
-    }
168
+        String[] tempArray = new String[array.length];
169
+        int newIdx = 0;
170
+        String last  = array[0];
171
+        String next = "";
172
+        String set = "";
173
+        for(int i = 0; i < array.length - 1; i++){
174
+             next = array[i+1];
175
+            if(next.equals(last)){
176
+                set += last;
177
+
178
+            } else {
179
+                tempArray[newIdx] = set;
180
+                set ="";
181
+                newIdx++;
182
+            }
153 183
 
184
+        }
154 185
 
186
+         return Arrays.copyOf(tempArray, newIdx + 1);
187
+        
188
+    }
155 189
 
156 190
 }
157 191