Bladeren bron

arrays done

Jennifer Chao 5 jaren geleden
bovenliggende
commit
ee317ea40f
1 gewijzigde bestanden met toevoegingen van 11 en 4 verwijderingen
  1. 11
    4
      src/main/java/rocks/zipcode/quiz5/arrays/ArrayUtils.java

+ 11
- 4
src/main/java/rocks/zipcode/quiz5/arrays/ArrayUtils.java Bestand weergeven

1
 package rocks.zipcode.quiz5.arrays;
1
 package rocks.zipcode.quiz5.arrays;
2
 
2
 
3
+import java.util.ArrayList;
4
+import java.util.Arrays;
5
+import java.util.List;
6
+
3
 /**
7
 /**
4
  * @author leon on 01/01/2019.
8
  * @author leon on 01/01/2019.
5
  */
9
  */
6
 public class ArrayUtils {
10
 public class ArrayUtils {
7
     public static String getMiddleElement(String[] values) {
11
     public static String getMiddleElement(String[] values) {
8
-        return null;
12
+        return values[values.length / 2];
9
     }
13
     }
10
 
14
 
11
     public static String[] removeMiddleElement(String[] values) {
15
     public static String[] removeMiddleElement(String[] values) {
12
-        return null;
16
+        List<String> list = new ArrayList<>(Arrays.asList(values));
17
+        list.remove(getMiddleElement(values));
18
+
19
+        return list.toArray(new String[0]);
13
     }
20
     }
14
 
21
 
15
     public static String getLastElement(String[] values) {
22
     public static String getLastElement(String[] values) {
16
-        return null;
23
+        return values[values.length - 1];
17
     }
24
     }
18
 
25
 
19
     public static String[] removeLastElement(String[] values) {
26
     public static String[] removeLastElement(String[] values) {
20
-        return null;
27
+        return Arrays.copyOfRange(values, 0, values.length - 1);
21
     }
28
     }
22
 }
29
 }