Nick Satinover 6 lat temu
rodzic
commit
2173f16c50
2 zmienionych plików z 52 dodań i 45 usunięć
  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 Wyświetl plik

@@ -1,6 +1,4 @@
1
-import java.util.ArrayList;
2
-import java.util.Arrays;
3
-import java.util.TreeMap;
1
+import java.util.*;
4 2
 
5 3
 public class Arraz {
6 4
 
@@ -73,24 +71,20 @@ public class Arraz {
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 89
     public int[] copyArrayByLoop(int[] ints){
96 90
         int[] retArr = new int[ints.length];
@@ -346,19 +340,25 @@ public class Arraz {
346 340
     }
347 341
 
348 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 347
         for (int i: arr) {
354 348
             if (i % 2 == 0){
355
-                even.add(i);
349
+                evenList.add(i);
356 350
             }
357 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 362
         int index = 0;
363 363
         for (int i: even) {
364 364
             retArr[index] = i;
@@ -369,7 +369,6 @@ public class Arraz {
369 369
             retArr[index] = i;
370 370
             index++;
371 371
         }
372
-
373 372
         return retArr;
374 373
     }
375 374
 }

+ 26
- 18
arraz/src/test/java/ArrazTest.java Wyświetl plik

@@ -224,23 +224,31 @@ public class ArrazTest {
224 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 253
     @Test
246 254
     public void test1copyArrayByLoop() {
@@ -744,7 +752,7 @@ public class ArrazTest {
744 752
         //THEN
745 753
         Assert.assertEquals(expected, actual);
746 754
     }
747
-    // sortArrayIntoEvensThenOdds
755
+
748 756
     @Test
749 757
     public void test1sortArrayIntoEvensThenOdds(){
750 758
         //GIVEN