Browse Source

StringArrayUtils

Jamez-s 6 years ago
parent
commit
4263a75411
2 changed files with 95 additions and 12 deletions
  1. 53
    12
      StringArrayUtils.java
  2. 42
    0
      package.bluej

+ 53
- 12
StringArrayUtils.java View File

@@ -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,8 @@ 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 16
     /**
@@ -17,7 +18,7 @@ public class StringArrayUtils {
17 18
      * @return second element in specified array
18 19
      */
19 20
     public static String getSecondElement(String[] array) {
20
-        return null;
21
+        return array[1];
21 22
     }
22 23
 
23 24
     /**
@@ -25,7 +26,7 @@ public class StringArrayUtils {
25 26
      * @return last element in specified array
26 27
      */ // TODO
27 28
     public static String getLastElement(String[] array) {
28
-        return null;
29
+        return array[array.length-1];
29 30
     }
30 31
 
31 32
     /**
@@ -33,7 +34,7 @@ public class StringArrayUtils {
33 34
      * @return second to last element in specified array
34 35
      */ // TODO
35 36
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
37
+        return array[array.length-2];
37 38
     }
38 39
 
39 40
     /**
@@ -42,7 +43,12 @@ public class StringArrayUtils {
42 43
      * @return true if the array contains the specified `value`
43 44
      */ // TODO
44 45
     public static boolean contains(String[] array, String value) {
45
-        return false;
46
+        boolean answer = false;
47
+        for (String string : array) {
48
+            if (string.equalsIgnoreCase(value));
49
+            answer = true;
50
+        }
51
+        return answer;
46 52
     }
47 53
 
48 54
     /**
@@ -50,7 +56,12 @@ 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
+        String[] answer = new String[array.length];
60
+
61
+        for (int i = 0; i< array.length; i++)
62
+            answer[i] = array[array.length-1-i];
63
+
64
+        return answer;
54 65
     }
55 66
 
56 67
     /**
@@ -58,7 +69,12 @@ public class StringArrayUtils {
58 69
      * @return true if the order of the array is the same backwards and forwards
59 70
      */ // TODO
60 71
     public static boolean isPalindromic(String[] array) {
61
-        return false;
72
+        boolean answer = false;
73
+        for (int i = 0; i < array.length; i++)
74
+            if (Arrays.equals(array, reverse(array))){
75
+                answer = true;
76
+            }
77
+        return answer;
62 78
     }
63 79
 
64 80
     /**
@@ -66,7 +82,17 @@ public class StringArrayUtils {
66 82
      * @return true if each letter in the alphabet has been used in the array
67 83
      */ // TODO
68 84
     public static boolean isPangramic(String[] array) {
69
-        return false;
85
+
86
+        boolean isPangramic = false;
87
+        for (char i = 'A' ; i <= 'Z'; i++)
88
+            if (Arrays.toString(array).indexOf(i) < 0 && (Arrays.toString(array).indexOf((char) i +32) < 0)) {
89
+                isPangramic = false;
90
+                break;
91
+            } else {
92
+                isPangramic = true;
93
+            }
94
+
95
+        return isPangramic;
70 96
     }
71 97
 
72 98
     /**
@@ -75,7 +101,13 @@ public class StringArrayUtils {
75 101
      * @return number of occurrences the specified `value` has occurred
76 102
      */ // TODO
77 103
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
104
+        int count = 0;
105
+        for (String s: array) {
106
+            if (value.equals(s)) {
107
+                count ++;
108
+            }
109
+        }
110
+        return count;     
79 111
     }
80 112
 
81 113
     /**
@@ -84,7 +116,16 @@ public class StringArrayUtils {
84 116
      * @return array with identical contents excluding values of `value`
85 117
      */ // TODO
86 118
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
119
+        int index = 0;
120
+        int count = getNumberOfOccurrences(array, valueToRemove);
121
+        String[] newArray = new String[array.length-count];       
122
+        for (int i = 0; i < array.length; i++) {
123
+            if (array[i] != valueToRemove) {
124
+                newArray[index] = array[i];
125
+                index++;
126
+            }        
127
+        }    
128
+        return newArray;
88 129
     }
89 130
 
90 131
     /**
@@ -92,6 +133,7 @@ public class StringArrayUtils {
92 133
      * @return array of Strings with consecutive duplicates removes
93 134
      */ // TODO
94 135
     public static String[] removeConsecutiveDuplicates(String[] array) {
136
+
95 137
         return null;
96 138
     }
97 139
 
@@ -103,5 +145,4 @@ public class StringArrayUtils {
103 145
         return null;
104 146
     }
105 147
 
106
-
107 148
 }

+ 42
- 0
package.bluej View File

@@ -0,0 +1,42 @@
1
+#BlueJ package file
2
+dependency1.from=StringArrayUtilsTest
3
+dependency1.to=StringArrayUtils
4
+dependency1.type=UsesDependency
5
+editor.fx.0.height=722
6
+editor.fx.0.width=800
7
+editor.fx.0.x=686
8
+editor.fx.0.y=23
9
+objectbench.height=101
10
+objectbench.width=461
11
+package.divider.horizontal=0.6
12
+package.divider.vertical=0.8007380073800738
13
+package.editor.height=427
14
+package.editor.width=674
15
+package.editor.x=387
16
+package.editor.y=81
17
+package.frame.height=600
18
+package.frame.width=800
19
+package.numDependencies=1
20
+package.numTargets=2
21
+package.showExtends=true
22
+package.showUses=true
23
+project.charset=UTF-8
24
+readme.height=58
25
+readme.name=@README
26
+readme.width=47
27
+readme.x=10
28
+readme.y=10
29
+target1.height=50
30
+target1.name=StringArrayUtilsTest
31
+target1.showInterface=false
32
+target1.type=ClassTarget
33
+target1.width=150
34
+target1.x=130
35
+target1.y=10
36
+target2.height=50
37
+target2.name=StringArrayUtils
38
+target2.showInterface=false
39
+target2.type=ClassTarget
40
+target2.width=120
41
+target2.x=70
42
+target2.y=70