Ver código fonte

wrote first five methods, passed tests

mpierse 6 anos atrás
pai
commit
226aa4dea5

+ 27
- 6
src/main/java/com/zipcodewilmington/StringArrayUtils.java Ver arquivo

@@ -1,5 +1,9 @@
1 1
 package com.zipcodewilmington;
2 2
 
3
+import java.util.Arrays;
4
+
5
+import static java.util.Arrays.binarySearch;
6
+
3 7
 /**
4 8
  * Created by leon on 1/29/18.
5 9
  */
@@ -9,7 +13,7 @@ public class StringArrayUtils {
9 13
      * @return first element of specified array
10 14
      */ // TODO
11 15
     public static String getFirstElement(String[] array) {
12
-        return null;
16
+        return array[0];
13 17
     }
14 18
 
15 19
     /**
@@ -17,7 +21,7 @@ public class StringArrayUtils {
17 21
      * @return second element in specified array
18 22
      */
19 23
     public static String getSecondElement(String[] array) {
20
-        return null;
24
+        return array[1];
21 25
     }
22 26
 
23 27
     /**
@@ -25,7 +29,7 @@ public class StringArrayUtils {
25 29
      * @return last element in specified array
26 30
      */ // TODO
27 31
     public static String getLastElement(String[] array) {
28
-        return null;
32
+        return array[array.length-1];
29 33
     }
30 34
 
31 35
     /**
@@ -33,7 +37,7 @@ public class StringArrayUtils {
33 37
      * @return second to last element in specified array
34 38
      */ // TODO
35 39
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
40
+        return array[array.length-2];
37 41
     }
38 42
 
39 43
     /**
@@ -42,15 +46,32 @@ public class StringArrayUtils {
42 46
      * @return true if the array contains the specified `value`
43 47
      */ // TODO
44 48
     public static boolean contains(String[] array, String value) {
45
-        return false;
49
+        boolean valuePresent = false;
50
+        for (int i=0; i<array.length; i++) {
51
+          if (array[i].equals(value)) {
52
+              valuePresent = true;
53
+                break;}
54
+        }
55
+        return valuePresent;
56
+
46 57
     }
47 58
 
59
+
60
+
48 61
     /**
49 62
      * @param array of String objects
50 63
      * @return an array with identical contents in reverse order
51 64
      */ // TODO
52 65
     public static String[] reverse(String[] array) {
53
-        return null;
66
+        int num = array.length;
67
+        String[] revArr;
68
+        revArr = String[num];
69
+        int cnt2= num-1;
70
+        for (int cnt1 =0; cnt1<num; cnt1++) {
71
+            array[cnt1]=revArr[cnt2];
72
+            cnt2--;
73
+        }
74
+        return revArr;
54 75
     }
55 76
 
56 77
     /**