Kaynağa Gözat

100% coverage

Jennifer Chao 6 yıl önce
ebeveyn
işleme
0959bc25c4
2 değiştirilmiş dosya ile 30 ekleme ve 41 silme
  1. 20
    24
      arraz/src/main/java/Arraz.java
  2. 10
    17
      arraz/src/test/java/ArrazTest.java

+ 20
- 24
arraz/src/main/java/Arraz.java Dosyayı Görüntüle

@@ -78,12 +78,9 @@ public class Arraz<T> {
78 78
 
79 79
     public T[] createEmptyGenericArray(T[] array, int length) {
80 80
         Class cl = array.getClass();
81
-        if (!cl.isArray()) {
82
-            return null;
83
-        } else {
84
-            T[] copy = (T[]) Array.newInstance(cl.getComponentType(), length);
85
-            return copy;
86
-        }
81
+        T[] copy = (T[]) Array.newInstance(cl.getComponentType(), length);
82
+
83
+        return copy;
87 84
     }
88 85
 
89 86
     public T[] copyArrayByIterator(T[] array) {
@@ -249,14 +246,14 @@ public class Arraz<T> {
249 246
         return newArray;
250 247
     }
251 248
 
252
-    public boolean check2ArraysForEquals(T[] array1, T[] array2){
249
+    public boolean check2ArraysForEquals(T[] array1, T[] array2) {
253 250
         if (Arrays.equals(array1, array2)) {
254 251
             return true;
255 252
         }
256 253
         return false;
257 254
     }
258 255
 
259
-    public double averageArrayWithoutMaxMin(int[] array){
256
+    public double averageArrayWithoutMaxMin(int[] array) {
260 257
         double sum = 0;
261 258
         int dupes = 0;
262 259
 
@@ -271,7 +268,7 @@ public class Arraz<T> {
271 268
         return sum / (array.length - dupes);
272 269
     }
273 270
 
274
-    public boolean arrayHas65And77(int[] array){
271
+    public boolean arrayHas65And77(int[] array) {
275 272
         if (containsValue(array, 65) && containsValue(array, 77)) {
276 273
             return true;
277 274
         }
@@ -279,7 +276,7 @@ public class Arraz<T> {
279 276
         return false;
280 277
     }
281 278
 
282
-    public boolean theTotalofTensIs30(int[] array){
279
+    public boolean theTotalofTensIs30(int[] array) {
283 280
         int sum = 0;
284 281
 
285 282
         for (int integer : array) {
@@ -296,19 +293,19 @@ public class Arraz<T> {
296 293
     }
297 294
 
298 295
 
299
-    public int findSmallestOfArray(int[] array){
296
+    public int findSmallestOfArray(int[] array) {
300 297
         return findMin(array);
301 298
     }
302 299
 
303
-    public int findSecondSmallestOfArray(int[] array){
300
+    public int findSecondSmallestOfArray(int[] array) {
304 301
         int indexOfSmallest = findIndexOf(array, findSmallestOfArray(array));
305 302
         array[indexOfSmallest] = findMax(array);
306 303
 
307 304
         return findMin(array);
308 305
     }
309 306
 
310
-    public int[] makeMeACopyPlease(int[] array){
311
-       return reverseArray(array);
307
+    public int[] makeMeACopyPlease(int[] array) {
308
+        return reverseArray(array);
312 309
     }
313 310
 
314 311
     public T[] removeFromIndex(T[] array, int index) {
@@ -348,21 +345,20 @@ public class Arraz<T> {
348 345
         return newArray;
349 346
     }
350 347
 
351
-    public List[] sortArrayIntoEvensThenOdds(int[] array){
352
-        List[] evensThenOdds = new ArrayList[2];
353
-        List<Integer> evens = new ArrayList<>();
354
-        List<Integer> odds = new ArrayList<>();
348
+    public ArrayList<Integer> sortArrayIntoEvensThenOdds(int[] array) {
349
+        ArrayList<Integer> arrayList = new ArrayList<>();
350
+        int indexOfEvens = 0;
355 351
 
356
-        for (int integer : array) {
357
-            if (integer % 2 == 0) {
358
-                evens.add(integer);
352
+        for (int integer: array) {
353
+            if (integer % 2 ==0) {
354
+                arrayList.add(indexOfEvens, integer);
355
+                indexOfEvens++;
359 356
             } else {
360
-                odds.add(integer);
357
+                arrayList.add(integer);
361 358
             }
362 359
         }
363 360
 
364
-        evensThenOdds[0] = evens; evensThenOdds[1] = odds;
365
-        return evensThenOdds;
361
+        return arrayList;
366 362
     }
367 363
 
368 364
 }

+ 10
- 17
arraz/src/test/java/ArrazTest.java Dosyayı Görüntüle

@@ -3,7 +3,6 @@ import org.junit.Before;
3 3
 import org.junit.Test;
4 4
 
5 5
 import java.util.ArrayList;
6
-import java.util.List;
7 6
 
8 7
 public class ArrazTest {
9 8
 
@@ -404,7 +403,8 @@ public class ArrazTest {
404 403
         original.add(92);
405 404
 
406 405
         Integer[] expected = new Integer[]{6, 20, 92};
407
-        Integer[] actual = (Integer[]) arraz.makeMeAnArrayFromArrayList(original, new Integer[0]);
406
+        Integer[] actual = new Integer[original.size()];
407
+        actual = (Integer[]) arraz.makeMeAnArrayFromArrayList(original, actual);
408 408
 
409 409
         Assert.assertEquals(expected, actual);
410 410
     }
@@ -416,7 +416,8 @@ public class ArrazTest {
416 416
         original.add("goodbye");
417 417
 
418 418
         String[] expected = new String[]{"hello", "goodbye"};
419
-        String[] actual = (String[]) arraz.makeMeAnArrayFromArrayList(original, new String[0]);
419
+        String[] actual = new String[original.size()];
420
+        actual = (String[]) arraz.makeMeAnArrayFromArrayList(original, new String[0]);
420 421
 
421 422
         Assert.assertEquals(expected, actual);
422 423
     }
@@ -652,26 +653,18 @@ public class ArrazTest {
652 653
     public void sortArrayIntoEvensThenOdds1() {
653 654
         int[] spiffyHandyIntArray = new int[] {4, 5, 102, 6, -7, 12, -32, 92, 8};
654 655
 
655
-        List<Integer> evens = new ArrayList<>();
656
-        evens.add(4); evens.add(102); evens.add(6); evens.add(12); evens.add(-32); evens.add(92); evens.add(8);
657
-        List<Integer> odds = new ArrayList<>();
658
-        odds.add(5); odds.add(-7);
659
-
660
-        List[] expected = new List[] {evens, odds};
661
-        List[] actual = arraz.sortArrayIntoEvensThenOdds(spiffyHandyIntArray);
656
+        ArrayList<Integer> expected = new ArrayList<>();
657
+        expected.add(4); expected.add(102); expected.add(6); expected.add(12); expected.add(-32); expected.add(92); expected.add(8); expected.add(5); expected.add(-7);
658
+        ArrayList<Integer> actual = arraz.sortArrayIntoEvensThenOdds(spiffyHandyIntArray);
662 659
 
663 660
         Assert.assertEquals(expected, actual);
664 661
     }
665 662
 
666 663
     @Test
667 664
     public void sortArrayIntoEvensThenOdds2() {
668
-        List<Integer> evens = new ArrayList<>();
669
-        evens.add(2); evens.add(4);
670
-        List<Integer> odds = new ArrayList<>();
671
-        odds.add(1); odds.add(3); odds.add(5);
672
-
673
-        List[] expected = new List[] {evens, odds};
674
-        List[] actual = arraz.sortArrayIntoEvensThenOdds(intArray);
665
+        ArrayList<Integer> expected = new ArrayList<>();
666
+        expected.add(2); expected.add(4); expected.add(1); expected.add(3); expected.add(5);
667
+        ArrayList<Integer> actual = arraz.sortArrayIntoEvensThenOdds(intArray);
675 668
 
676 669
         Assert.assertEquals(expected, actual);
677 670
     }