Joshua Chung пре 8 година
родитељ
комит
f511613422
2 измењених фајлова са 38 додато и 54 уклоњено
  1. 38
    10
      StringArrayUtils.java
  2. 0
    44
      StringArrayUtilsTest.java

+ 38
- 10
StringArrayUtils.java Прегледај датотеку

1
- 
1
+import java.util.Arrays;
2
 
2
 
3
 /**
3
 /**
4
  * Created by leon on 1/29/18.
4
  * Created by leon on 1/29/18.
9
      * @return first element of specified array
9
      * @return first element of specified array
10
      */ // TODO
10
      */ // TODO
11
     public static String getFirstElement(String[] array) {
11
     public static String getFirstElement(String[] array) {
12
-        return null;
12
+        return array[0];
13
     }
13
     }
14
 
14
 
15
     /**
15
     /**
17
      * @return second element in specified array
17
      * @return second element in specified array
18
      */
18
      */
19
     public static String getSecondElement(String[] array) {
19
     public static String getSecondElement(String[] array) {
20
-        return null;
20
+        return array[1];
21
     }
21
     }
22
 
22
 
23
     /**
23
     /**
24
      * @param array array of String objects
24
      * @param array array of String objects
25
      * @return last element in specified array
25
      * @return last element in specified array
26
      */ // TODO
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
      * @return second to last element in specified array
33
      * @return second to last element in specified array
34
      */ // TODO
34
      */ // TODO
35
     public static String getSecondToLastElement(String[] array) {
35
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
36
+        return array[array.length - 2];
37
     }
37
     }
38
 
38
 
39
     /**
39
     /**
41
      * @param value value to check array for
41
      * @param value value to check array for
42
      * @return true if the array contains the specified `value`
42
      * @return true if the array contains the specified `value`
43
      */ // TODO
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
         return false;
51
         return false;
46
     }
52
     }
47
 
53
 
50
      * @return an array with identical contents in reverse order
56
      * @return an array with identical contents in reverse order
51
      */ // TODO
57
      */ // TODO
52
     public static String[] reverse(String[] array) {
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
      * @return true if the order of the array is the same backwards and forwards
70
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
71
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
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
         return false;
84
         return false;
62
     }
85
     }
63
 
86
 
87
+
88
+
64
     /**
89
     /**
65
      * @param array array of String objects
90
      * @param array array of String objects
66
      * @return true if each letter in the alphabet has been used in the array
91
      * @return true if each letter in the alphabet has been used in the array
92
      * @return array of Strings with consecutive duplicates removes
117
      * @return array of Strings with consecutive duplicates removes
93
      */ // TODO
118
      */ // TODO
94
     public static String[] removeConsecutiveDuplicates(String[] array) {
119
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
96
-    }
120
+        
97
 
121
 
122
+        
123
+        return null;    
124
+    }
125
+    
98
     /**
126
     /**
99
      * @param array array of chars
127
      * @param array array of chars
100
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
128
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings

+ 0
- 44
StringArrayUtilsTest.java Прегледај датотеку

33
         Assert.assertEquals(expected, actual);
33
         Assert.assertEquals(expected, actual);
34
     }
34
     }
35
 
35
 
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
     @Test
36
     @Test
50
     public void testGetSecondElement1() {
37
     public void testGetSecondElement1() {
51
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
38
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
62
         Assert.assertEquals(expected, actual);
49
         Assert.assertEquals(expected, actual);
63
     }
50
     }
64
 
51
 
65
-
66
     @Test
52
     @Test
67
     public void testGetSecondElement3() {
53
     public void testGetSecondElement3() {
68
         String[] array = {"brown", "fox", "jumps", "over", "the", "lazy", "dog"};
54
         String[] array = {"brown", "fox", "jumps", "over", "the", "lazy", "dog"};
71
         Assert.assertEquals(expected, actual);
57
         Assert.assertEquals(expected, actual);
72
     }
58
     }
73
 
59
 
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
     @Test
60
     @Test
85
     public void testGetLastElement1() {
61
     public void testGetLastElement1() {
86
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
62
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
106
         Assert.assertEquals(expected, actual);
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
     @Test
85
     @Test
130
     public void testGetSecondToLastElement1() {
86
     public void testGetSecondToLastElement1() {
131
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};
87
         String[] array = {"the", "quick", "brown", "fox", "jumps", "over", "the", "lazy", "dog"};