Przeglądaj źródła

Completed string arrays lab

Jonathan Hinds 6 lat temu
rodzic
commit
83bc385881

+ 123
- 10
src/main/java/com/zipcodewilmington/StringArrayUtils.java Wyświetl plik

1
 package com.zipcodewilmington;
1
 package com.zipcodewilmington;
2
 
2
 
3
+import java.lang.reflect.Array;
4
+import java.util.ArrayList;
5
+import java.util.Arrays;
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) {
49
+        for(String word : array){
50
+            if(word.equals(value))
51
+            {
52
+                return true;
53
+            }
54
+        }
45
         return false;
55
         return false;
46
     }
56
     }
47
 
57
 
50
      * @return an array with identical contents in reverse order
60
      * @return an array with identical contents in reverse order
51
      */ // TODO
61
      */ // TODO
52
     public static String[] reverse(String[] array) {
62
     public static String[] reverse(String[] array) {
53
-        return null;
63
+        //create a new array the same length as the new array
64
+        String[] reverseArray = new String[array.length];
65
+        //set a counter which iterates the new array from the begining
66
+        int counter = 0;
67
+        //starting from the back of the array, increment backwards towards and including 0
68
+        for(int i = array.length - 1; i >= 0; i --)
69
+        {
70
+            //assign the value of array at i, (the back of the array) to the front of the new array
71
+            reverseArray[counter] = array[i];
72
+            //increase the counter w
73
+            counter ++;
74
+        }
75
+        return reverseArray;
54
     }
76
     }
55
 
77
 
56
     /**
78
     /**
58
      * @return true if the order of the array is the same backwards and forwards
80
      * @return true if the order of the array is the same backwards and forwards
59
      */ // TODO
81
      */ // TODO
60
     public static boolean isPalindromic(String[] array) {
82
     public static boolean isPalindromic(String[] array) {
83
+        //loop through the array
84
+        for(int i = 0; i < array.length; i++){
85
+            if(array[i].equals(array[(array.length -1) - i])){
86
+                if(i == array.length -1){
87
+                    return true;
88
+                }
89
+            } else {
90
+                break;
91
+            }
92
+        }
61
         return false;
93
         return false;
62
     }
94
     }
63
 
95
 
66
      * @return true if each letter in the alphabet has been used in the array
98
      * @return true if each letter in the alphabet has been used in the array
67
      */ // TODO
99
      */ // TODO
68
     public static boolean isPangramic(String[] array) {
100
     public static boolean isPangramic(String[] array) {
101
+
102
+        //array list to add letters to
103
+        ArrayList<Character> alpha = new ArrayList<Character>();
104
+        //turn the array of strings into lowercase since uppercase and lowercase have different codes
105
+        String arrayString = Arrays.toString(array).toLowerCase();
106
+        //conver the string to an array of characters
107
+        char[] letters = arrayString.toCharArray();
108
+        //loop through each character
109
+        for(char letter : letters)
110
+        {
111
+            //if the character is already in the array, and is a letter / not symbol
112
+            if(alpha.contains(letter) && (letter <= 122 && letter >= 97)){
113
+                //skip this letter
114
+                continue;
115
+            //f the character is not in the array, and the is a letter / not a symbol
116
+            } else if(!alpha.contains(letter) && (letter <= 122 && letter >= 97)) {
117
+                //add this letter
118
+                alpha.add(letter);
119
+            }
120
+        }
121
+        //if the size of the array is 26 (all letters accounted for) return true
122
+        if(alpha.size() == 26){
123
+            return true;
124
+        }
125
+        //else return false
69
         return false;
126
         return false;
70
     }
127
     }
71
 
128
 
75
      * @return number of occurrences the specified `value` has occurred
132
      * @return number of occurrences the specified `value` has occurred
76
      */ // TODO
133
      */ // TODO
77
     public static int getNumberOfOccurrences(String[] array, String value) {
134
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
135
+        int numberOfTimes = 0;
136
+        for(String word : array){
137
+            if(word.equals(value)){numberOfTimes ++;}
138
+        }
139
+        return numberOfTimes;
79
     }
140
     }
80
 
141
 
81
     /**
142
     /**
84
      * @return array with identical contents excluding values of `value`
145
      * @return array with identical contents excluding values of `value`
85
      */ // TODO
146
      */ // TODO
86
     public static String[] removeValue(String[] array, String valueToRemove) {
147
     public static String[] removeValue(String[] array, String valueToRemove) {
87
-        return null;
148
+
149
+        ArrayList<String> valulessList = new ArrayList<String>();
150
+
151
+        for(String values : array)
152
+        {
153
+            if(values.equals(valueToRemove))
154
+            {
155
+                continue;
156
+            } else {
157
+                valulessList.add(values);
158
+            }
159
+        }
160
+
161
+        String[] newArray = valulessList.toArray(new String[valulessList.size() - 1]);
162
+        return newArray;
88
     }
163
     }
89
 
164
 
90
     /**
165
     /**
92
      * @return array of Strings with consecutive duplicates removes
167
      * @return array of Strings with consecutive duplicates removes
93
      */ // TODO
168
      */ // TODO
94
     public static String[] removeConsecutiveDuplicates(String[] array) {
169
     public static String[] removeConsecutiveDuplicates(String[] array) {
95
-        return null;
170
+
171
+        ArrayList<String> consecString = new ArrayList<String>();
172
+
173
+        for(int i = 1; i < array.length; i ++){
174
+            String word = array[i - 1];
175
+            String next = array[i];
176
+
177
+            if(word.equals(next))
178
+            {
179
+                if(!consecString.contains(word)){
180
+                    consecString.add(word);
181
+                } else if(!consecString.get(consecString.size() - 1).equals(word) && consecString.contains(word)){
182
+                    consecString.add(word);
183
+                }
184
+            } else {
185
+                consecString.add(next);
186
+            }
187
+        }
188
+
189
+        String[] newArray = consecString.toArray(new String[consecString.size() - 1]);
190
+        System.out.println(Arrays.toString(newArray));
191
+        return newArray;
96
     }
192
     }
97
 
193
 
98
     /**
194
     /**
100
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
196
      * @return array of Strings with each consecutive duplicate occurrence concatenated as a single string in an array of Strings
101
      */ // TODO
197
      */ // TODO
102
     public static String[] packConsecutiveDuplicates(String[] array) {
198
     public static String[] packConsecutiveDuplicates(String[] array) {
103
-        return null;
104
-    }
105
 
199
 
200
+        ArrayList<String> consecString = new ArrayList<String>();
201
+        String last = array[0];
202
+        consecString.add(array[0]);
203
+        //for each word
204
+        for(int i = 1; i < array.length; i ++){
205
+            String next = array[i];
206
+            //if the words are equal
207
+            if(next.equals(last))
208
+            {
209
+                consecString.set(consecString.size()-1, consecString.get(consecString.size()-1) + next);
210
+            } else {
211
+                consecString.add(next);
212
+                last = next;
213
+            }
214
+        }
106
 
215
 
216
+        String[] newArray = consecString.toArray(new String[consecString.size() - 1]);
217
+        System.out.println(Arrays.toString(newArray));
218
+        return newArray;
219
+    }
107
 }
220
 }