浏览代码

StringArrayUtils

Jamez-s 8 年前
父节点
当前提交
4263a75411
共有 2 个文件被更改,包括 95 次插入12 次删除
  1. 53
    12
      StringArrayUtils.java
  2. 42
    0
      package.bluej

+ 53
- 12
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
     /**
16
     /**
17
      * @return second element in specified array
18
      * @return second element in specified array
18
      */
19
      */
19
     public static String getSecondElement(String[] array) {
20
     public static String getSecondElement(String[] array) {
20
-        return null;
21
+        return array[1];
21
     }
22
     }
22
 
23
 
23
     /**
24
     /**
25
      * @return last element in specified array
26
      * @return last element in specified array
26
      */ // TODO
27
      */ // TODO
27
     public static String getLastElement(String[] array) {
28
     public static String getLastElement(String[] array) {
28
-        return null;
29
+        return array[array.length-1];
29
     }
30
     }
30
 
31
 
31
     /**
32
     /**
33
      * @return second to last element in specified array
34
      * @return second to last element in specified array
34
      */ // TODO
35
      */ // TODO
35
     public static String getSecondToLastElement(String[] array) {
36
     public static String getSecondToLastElement(String[] array) {
36
-        return null;
37
+        return array[array.length-2];
37
     }
38
     }
38
 
39
 
39
     /**
40
     /**
42
      * @return true if the array contains the specified `value`
43
      * @return true if the array contains the specified `value`
43
      */ // TODO
44
      */ // TODO
44
     public static boolean contains(String[] array, String value) {
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
      * @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
+        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
      * @return true if the order of the array is the same backwards and forwards
69
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
70
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
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
      * @return true if each letter in the alphabet has been used in the array
82
      * @return true if each letter in the alphabet has been used in the array
67
      */ // TODO
83
      */ // TODO
68
     public static boolean isPangramic(String[] array) {
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
      * @return number of occurrences the specified `value` has occurred
101
      * @return number of occurrences the specified `value` has occurred
76
      */ // TODO
102
      */ // TODO
77
     public static int getNumberOfOccurrences(String[] array, String value) {
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
      * @return array with identical contents excluding values of `value`
116
      * @return array with identical contents excluding values of `value`
85
      */ // TODO
117
      */ // TODO
86
     public static String[] removeValue(String[] array, String valueToRemove) {
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
      * @return array of Strings with consecutive duplicates removes
133
      * @return array of Strings with consecutive duplicates removes
93
      */ // TODO
134
      */ // TODO
94
     public static String[] removeConsecutiveDuplicates(String[] array) {
135
     public static String[] removeConsecutiveDuplicates(String[] array) {
136
+
95
         return null;
137
         return null;
96
     }
138
     }
97
 
139
 
103
         return null;
145
         return null;
104
     }
146
     }
105
 
147
 
106
-
107
 }
148
 }

+ 42
- 0
package.bluej 查看文件

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