Browse Source

wrote first five methods, passed tests

mpierse 6 years ago
parent
commit
226aa4dea5
1 changed files with 27 additions and 6 deletions
  1. 27
    6
      src/main/java/com/zipcodewilmington/StringArrayUtils.java

+ 27
- 6
src/main/java/com/zipcodewilmington/StringArrayUtils.java View File

1
 package com.zipcodewilmington;
1
 package com.zipcodewilmington;
2
 
2
 
3
+import java.util.Arrays;
4
+
5
+import static java.util.Arrays.binarySearch;
6
+
3
 /**
7
 /**
4
  * Created by leon on 1/29/18.
8
  * Created by leon on 1/29/18.
5
  */
9
  */
9
      * @return first element of specified array
13
      * @return first element of specified array
10
      */ // TODO
14
      */ // TODO
11
     public static String getFirstElement(String[] array) {
15
     public static String getFirstElement(String[] array) {
12
-        return null;
16
+        return array[0];
13
     }
17
     }
14
 
18
 
15
     /**
19
     /**
17
      * @return second element in specified array
21
      * @return second element in specified array
18
      */
22
      */
19
     public static String getSecondElement(String[] array) {
23
     public static String getSecondElement(String[] array) {
20
-        return null;
24
+        return array[1];
21
     }
25
     }
22
 
26
 
23
     /**
27
     /**
25
      * @return last element in specified array
29
      * @return last element in specified array
26
      */ // TODO
30
      */ // TODO
27
     public static String getLastElement(String[] array) {
31
     public static String getLastElement(String[] array) {
28
-        return null;
32
+        return array[array.length-1];
29
     }
33
     }
30
 
34
 
31
     /**
35
     /**
33
      * @return second to last element in specified array
37
      * @return second to last element in specified array
34
      */ // TODO
38
      */ // TODO
35
     public static String getSecondToLastElement(String[] array) {
39
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
40
+        return array[array.length-2];
37
     }
41
     }
38
 
42
 
39
     /**
43
     /**
42
      * @return true if the array contains the specified `value`
46
      * @return true if the array contains the specified `value`
43
      */ // TODO
47
      */ // TODO
44
     public static boolean contains(String[] array, String value) {
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
      * @param array of String objects
62
      * @param array of String objects
50
      * @return an array with identical contents in reverse order
63
      * @return an array with identical contents in reverse order
51
      */ // TODO
64
      */ // TODO
52
     public static String[] reverse(String[] array) {
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
     /**