Просмотр исходного кода

Tidy up binary search hints and reference implementation

Frida Tveit 9 лет назад
Родитель
Сommit
8b9a7a21b4

+ 3
- 2
exercises/binary-search/.meta/hints.md Просмотреть файл

@@ -11,14 +11,15 @@ You have to specify which type you want to put into the class when you construct
11 11
 
12 12
 For example you could construct a list of `Integers`:
13 13
 
14
-`List<Integer> someList = new LinkedList();`
14
+`List<Integer> someList = new LinkedList<>();`
15 15
 
16 16
 Now `someList` can only contain `Integers`. You could also do:
17 17
 
18
-`List<String> someOtherList = new LinkedList()`
18
+`List<String> someOtherList = new LinkedList<>()`
19 19
 
20 20
 Now `someOtherList` can only contain `Strings`.
21 21
 
22 22
 Another constraint is that any type used with generics cannot be a [primitive type](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html), such as `int` or `long`.
23
+However, every primitive type has a corresponding reference type, so instead of `int` you can use [`Integer`](https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html) and instead of `long` you can use [`Long`](https://docs.oracle.com/javase/8/docs/api/java/lang/Long.html).
23 24
 
24 25
 It can help to look at an [example use case of generics](https://docs.oracle.com/javase/tutorial/java/generics/types.html) to get you started.

+ 3
- 7
exercises/binary-search/.meta/src/reference/java/BinarySearch.java Просмотреть файл

@@ -1,24 +1,20 @@
1 1
 
2 2
 import java.util.List;
3 3
 
4
-public class BinarySearch<T extends Comparable<T>> {
4
+class BinarySearch<T extends Comparable<T>> {
5 5
 
6 6
     private List<T> array;
7 7
     private int arraySize;
8 8
 
9
-    public BinarySearch(List<T> array) {
9
+    BinarySearch(List<T> array) {
10 10
         this.array = array;
11 11
         this.arraySize = array.size();
12 12
     }
13 13
 
14
-    public int indexOf(T value) {
14
+    int indexOf(T value) {
15 15
         return search(value);
16 16
     }
17 17
 
18
-    public List<T> getArray() {
19
-        return array;
20
-    }
21
-
22 18
     private int search(T value) {
23 19
         int left = 0;
24 20
         int right = this.arraySize - 1;