Ver código fonte

Tidy up binary search hints and reference implementation

Frida Tveit 9 anos atrás
pai
commit
8b9a7a21b4

+ 3
- 2
exercises/binary-search/.meta/hints.md Ver arquivo

11
 
11
 
12
 For example you could construct a list of `Integers`:
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
 Now `someList` can only contain `Integers`. You could also do:
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
 Now `someOtherList` can only contain `Strings`.
20
 Now `someOtherList` can only contain `Strings`.
21
 
21
 
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`.
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
 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.
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 Ver arquivo

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