Pārlūkot izejas kodu

More then half is done, still need to do the harder ones

nmaidanos 8 gadus atpakaļ
vecāks
revīzija
e80f0026dc
1 mainītis faili ar 22 papildinājumiem un 8 dzēšanām
  1. 22
    8
      StringArrayUtils.java

+ 22
- 8
StringArrayUtils.java Parādīt failu

1
- 
1
+ import java.util.*;
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
     /**
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) {
27
     public static String getLastElement(String[] array) {
28
-        return null;
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
     /**
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
-        return false;
45
+        return Arrays.asList(array).contains(value);
46
     }
46
     }
47
 
47
 
48
     /**
48
     /**
50
      * @return an array with identical contents in reverse order
50
      * @return an array with identical contents in reverse order
51
      */ // TODO
51
      */ // TODO
52
     public static String[] reverse(String[] array) {
52
     public static String[] reverse(String[] array) {
53
-        return null;
53
+        
54
+        for (int i = 0; i < array.length/2; i++){
55
+            String temp = array[i];
56
+            array[i] = array[(array.length -1) - i];
57
+            array[(array.length -1) - i] = temp;  
58
+        }
59
+        
60
+        return array;
61
+        
54
     }
62
     }
55
 
63
 
56
     /**
64
     /**
75
      * @return number of occurrences the specified `value` has occurred
83
      * @return number of occurrences the specified `value` has occurred
76
      */ // TODO
84
      */ // TODO
77
     public static int getNumberOfOccurrences(String[] array, String value) {
85
     public static int getNumberOfOccurrences(String[] array, String value) {
78
-        return 0;
86
+        int counter = 0;
87
+        for(String str: array){
88
+            if(str.equals(value)) {
89
+                counter++;
90
+            }
91
+        }
92
+        return counter;
79
     }
93
     }
80
 
94
 
81
     /**
95
     /**