Bläddra i källkod

updated commit

Joshua Chung 8 år sedan
förälder
incheckning
f511613422
2 ändrade filer med 38 tillägg och 54 borttagningar
  1. 38
    10
      StringArrayUtils.java
  2. 0
    44
      StringArrayUtilsTest.java

+ 38
- 10
StringArrayUtils.java Visa fil

@@ -1,4 +1,4 @@
1
- 
1
+import java.util.Arrays;
2 2
 
3 3
 /**
4 4
  * Created by leon on 1/29/18.
@@ -9,7 +9,7 @@ public class StringArrayUtils {
9 9
      * @return first element of specified array
10 10
      */ // TODO
11 11
     public static String getFirstElement(String[] array) {
12
-        return null;
12
+        return array[0];
13 13
     }
14 14
 
15 15
     /**
@@ -17,15 +17,15 @@ public class StringArrayUtils {
17 17
      * @return second element in specified array
18 18
      */
19 19
     public static String getSecondElement(String[] array) {
20
-        return null;
20
+        return array[1];
21 21
     }
22 22
 
23 23
     /**
24 24
      * @param array array of String objects
25 25
      * @return last element in specified array
26 26
      */ // TODO
27
-    public static String getLastElement(String[] array) {
28
-        return null;
27
+    public static String getLastElement(String[] array) {        
28
+        return array[array.length - 1];
29 29
     }
30 30
 
31 31
     /**
@@ -33,7 +33,7 @@ public class StringArrayUtils {
33 33
      * @return second to last element in specified array
34 34
      */ // TODO
35 35
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
36
+        return array[array.length - 2];
37 37
     }
38 38
 
39 39
     /**
@@ -41,7 +41,13 @@ public class StringArrayUtils {
41 41
      * @param value value to check array for
42 42
      * @return true if the array contains the specified `value`
43 43
      */ // TODO
44
-    public static boolean contains(String[] array, String value) {
44
+    public static boolean contains(String[] array, String value) {        
45
+        
46
+        for(String x : array) {
47
+            if(value == x) {
48
+                return true;
49
+            }
50
+        }
45 51
         return false;
46 52
     }
47 53
 
@@ -50,7 +56,13 @@ public class StringArrayUtils {
50 56
      * @return an array with identical contents in reverse order
51 57
      */ // TODO
52 58
     public static String[] reverse(String[] array) {
53
-        return null;
59
+        
60
+        String[] reverseArr = new String[array.length];
61
+        
62
+        for(int i=0; i<array.length; i++){            
63
+            reverseArr[i] = array[array.length - i - 1];
64
+        }
65
+        return reverseArr;
54 66
     }
55 67
 
56 68
     /**
@@ -58,9 +70,22 @@ public class StringArrayUtils {
58 70
      * @return true if the order of the array is the same backwards and forwards
59 71
      */ // TODO
60 72
     public static boolean isPalindromic(String[] array) {
73
+        
74
+        String[] reverseArr = reverse(array);
75
+        String[] regArr = new String[array.length];
76
+        
77
+        for(int i=0; i<array.length; i++){
78
+             regArr[i] = array[i];
79
+        }
80
+        
81
+        if(regArr ==  reverseArr) {
82
+            return true;
83
+        }
61 84
         return false;
62 85
     }
63 86
 
87
+
88
+
64 89
     /**
65 90
      * @param array array of String objects
66 91
      * @return true if each letter in the alphabet has been used in the array
@@ -92,9 +117,12 @@ public class StringArrayUtils {
92 117
      * @return array of Strings with consecutive duplicates removes
93 118
      */ // TODO
94 119
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
96
-    }
120
+        
97 121
 
122
+        
123
+        return null;    
124
+    }
125
+    
98 126
     /**
99 127
      * @param array array of chars
100 128
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings

+ 0
- 44
StringArrayUtilsTest.java Visa fil

@@ -33,19 +33,6 @@ public class StringArrayUtilsTest {
33 33
         Assert.assertEquals(expected, actual);
34 34
     }
35 35
 
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49 36
     @Test
50 37
     public void testGetSecondElement1() {
51 38
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -62,7 +49,6 @@ public class StringArrayUtilsTest {
62 49
         Assert.assertEquals(expected, actual);
63 50
     }
64 51
 
65
-
66 52
     @Test
67 53
     public void testGetSecondElement3() {
68 54
         String[] array = {"brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -71,16 +57,6 @@ public class StringArrayUtilsTest {
71 57
         Assert.assertEquals(expected, actual);
72 58
     }
73 59
 
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84 60
     @Test
85 61
     public void testGetLastElement1() {
86 62
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
@@ -106,26 +82,6 @@ public class StringArrayUtilsTest {
106 82
         Assert.assertEquals(expected, actual);
107 83
     }
108 84
 
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129 85
     @Test
130 86
     public void testGetSecondToLastElement1() {
131 87
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};