Bladeren bron

Completed

Nick Satinover 6 jaren geleden
bovenliggende
commit
2173f16c50
2 gewijzigde bestanden met toevoegingen van 52 en 45 verwijderingen
  1. 26
    27
      arraz/src/main/java/Arraz.java
  2. 26
    18
      arraz/src/test/java/ArrazTest.java

+ 26
- 27
arraz/src/main/java/Arraz.java Bestand weergeven

1
-import java.util.ArrayList;
2
-import java.util.Arrays;
3
-import java.util.TreeMap;
1
+import java.util.*;
4
 
2
 
5
 public class Arraz {
3
 public class Arraz {
6
 
4
 
73
     }
71
     }
74
 
72
 
75
 
73
 
76
-//    public Integer[] copyArrayByIterator(Integer[] intInput) {
77
-//        Integer[] retArr = new Integer[intInput.length];
78
-//
79
-//        Iterator<Integer> iterator = new Iterator<Integer>() {
80
-//            public boolean hasNext(int intValue) {
81
-//                return intValue == null ? true : false;
82
-//            }
83
-//
84
-//            public Integer next() {
85
-//                return null;
86
-//            }
87
-//
88
-//            public void remove() {
89
-//
90
-//            }
91
-//        }
92
-//        return retArr;
93
-//    }
74
+    public int[] copyArrayByIterator(Integer[] intInput) {
75
+        int[] retArr = new int[intInput.length];
76
+        List<Integer> list = Arrays.asList(intInput);
77
+        Iterable<Integer> iterable = list;
78
+        Iterator<Integer> iterator = iterable.iterator();
79
+
80
+        int index = 0;
81
+        while (index < intInput.length){
82
+            retArr[index] = iterator.next();
83
+            index++;
84
+        }
85
+
86
+        return retArr;
87
+    }
94
 
88
 
95
     public int[] copyArrayByLoop(int[] ints){
89
     public int[] copyArrayByLoop(int[] ints){
96
         int[] retArr = new int[ints.length];
90
         int[] retArr = new int[ints.length];
346
     }
340
     }
347
 
341
 
348
     public int[] sortArrayIntoEvensThenOdds(int[] arr) {
342
     public int[] sortArrayIntoEvensThenOdds(int[] arr) {
349
-        ArrayList<Integer> even = new ArrayList<>();
350
-        ArrayList<Integer> odd = new ArrayList<>();
351
-        int[] retArr = new int[arr.length];
343
+        ArrayList<Integer> evenList = new ArrayList<>();
344
+        ArrayList<Integer> oddList = new ArrayList<>();
345
+        int retArrLength = arr.length;
352
 
346
 
353
         for (int i: arr) {
347
         for (int i: arr) {
354
             if (i % 2 == 0){
348
             if (i % 2 == 0){
355
-                even.add(i);
349
+                evenList.add(i);
356
             }
350
             }
357
             else {
351
             else {
358
-                odd.add(i);
352
+                oddList.add(i);
359
             }
353
             }
360
         }
354
         }
361
 
355
 
356
+        return retArr(evenList, oddList, retArrLength);
357
+    }
358
+
359
+    private int[] retArr(ArrayList<Integer> even, ArrayList<Integer> odd, int retArrLength){
360
+        int[] retArr = new int[retArrLength];
361
+
362
         int index = 0;
362
         int index = 0;
363
         for (int i: even) {
363
         for (int i: even) {
364
             retArr[index] = i;
364
             retArr[index] = i;
369
             retArr[index] = i;
369
             retArr[index] = i;
370
             index++;
370
             index++;
371
         }
371
         }
372
-
373
         return retArr;
372
         return retArr;
374
     }
373
     }
375
 }
374
 }

+ 26
- 18
arraz/src/test/java/ArrazTest.java Bestand weergeven

224
         Assert.assertEquals(expected, actual);
224
         Assert.assertEquals(expected, actual);
225
     }
225
     }
226
 
226
 
227
-//    @Test
228
-//    public void test1copyArrayByIterator() {
229
-//        //GIVEN
230
-//        Object[] arr = {1, 2, 3, 4, 5};
231
-//        //WHEN
232
-//        Object[] retObject = arraz.copyArrayByIterator(arr);
233
-//        int expected = 3;
234
-//
235
-//        //THEN
236
-//        int actual = retObject.;
237
-//        Assert.assertEquals(expected, actual);
238
-//    }
239
-//
240
-//    @Test
241
-//    public void test2copyArrayByIterator() {
242
-//
243
-//    }
227
+    @Test
228
+    public void test1copyArrayByIterator() {
229
+        //GIVEN
230
+        Integer[] arr = {1, 2, 3, 4, 5};
231
+        //WHEN
232
+        int[] retArr = arraz.copyArrayByIterator(arr);
233
+        int expected = 3;
234
+
235
+        //THEN
236
+        int actual = retArr[2];
237
+        Assert.assertEquals(expected, actual);
238
+    }
239
+
240
+    @Test
241
+    public void test2copyArrayByIterator() {
242
+        //GIVEN
243
+        Integer[] arr = {1, 2, 3, 4, 5};
244
+        //WHEN
245
+        int[] retArr = arraz.copyArrayByIterator(arr);
246
+        int expected = 5;
247
+
248
+        //THEN
249
+        int actual = retArr[4];
250
+        Assert.assertEquals(expected, actual);
251
+    }
244
 
252
 
245
     @Test
253
     @Test
246
     public void test1copyArrayByLoop() {
254
     public void test1copyArrayByLoop() {
744
         //THEN
752
         //THEN
745
         Assert.assertEquals(expected, actual);
753
         Assert.assertEquals(expected, actual);
746
     }
754
     }
747
-    // sortArrayIntoEvensThenOdds
755
+
748
     @Test
756
     @Test
749
     public void test1sortArrayIntoEvensThenOdds(){
757
     public void test1sortArrayIntoEvensThenOdds(){
750
         //GIVEN
758
         //GIVEN