Selaa lähdekoodia

Merge pull request #868 from FridaTveit/BinarySearchUseGenerics

binary-search: use generics
Stuart Kent 9 vuotta sitten
vanhempi
commit
b1a0b1de43

+ 12
- 12
config.json Näytä tiedosto

@@ -655,18 +655,6 @@
655 655
       "uuid": "76d28d97-75d3-47eb-bb77-3d347b76f1b6"
656 656
     },
657 657
     {
658
-      "core": true,
659
-      "difficulty": 6,
660
-      "slug": "linked-list",
661
-      "topics": [
662
-        "algorithms",
663
-        "lists",
664
-        "generics"
665
-      ],
666
-      "unlocked_by": null,
667
-      "uuid": "7ba5084d-3b75-4406-a0d7-87c26387f9c0"
668
-    },
669
-    {
670 658
       "core": false,
671 659
       "difficulty": 6,
672 660
       "slug": "grade-school",
@@ -765,6 +753,18 @@
765 753
       "uuid": "4b3f7771-c642-4278-a3d9-2fb958f26361"
766 754
     },
767 755
     {
756
+      "core": true,
757
+      "difficulty": 6,
758
+      "slug": "linked-list",
759
+      "topics": [
760
+        "algorithms",
761
+        "lists",
762
+        "generics"
763
+      ],
764
+      "unlocked_by": null,
765
+      "uuid": "7ba5084d-3b75-4406-a0d7-87c26387f9c0"
766
+    },
767
+    {
768 768
       "core": false,
769 769
       "difficulty": 6,
770 770
       "slug": "tournament",

+ 25
- 0
exercises/binary-search/.meta/hints.md Näytä tiedosto

@@ -0,0 +1,25 @@
1
+This exercise introduces [generics](https://docs.oracle.com/javase/tutorial/java/generics/index.html).
2
+To make the tests pass you need to construct your class such that it accepts any type of input, e.g. `Integer` or `String`.
3
+
4
+Generics are useful because they allow you to write more general and reusable code.
5
+The Java [List](https://docs.oracle.com/javase/8/docs/api/java/util/List.html) and [Map](https://docs.oracle.com/javase/8/docs/api/java/util/Map.html) implementations are both examples of classes that use generics.
6
+By using them you can construct a `List` containing `Integers` or a list containing `Strings` or any other type.
7
+
8
+There are a few constraints on the types used in generics.
9
+One of them is that once you've constructed a `List` containing `Integers`, you can't put `Strings` into it.
10
+You have to specify which type you want to put into the class when you construct it, and that instance can then only be used with that type.
11
+
12
+For example you could construct a list of `Integers`:
13
+
14
+`List<Integer> someList = new LinkedList<>();`
15
+
16
+Now `someList` can only contain `Integers`. You could also do:
17
+
18
+`List<String> someOtherList = new LinkedList<>()`
19
+
20
+Now `someOtherList` can only contain `Strings`.
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`.
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).
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 Näytä tiedosto

@@ -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;

+ 11
- 11
exercises/binary-search/src/test/java/BinarySearchTest.java Näytä tiedosto

@@ -13,23 +13,23 @@ public class BinarySearchTest {
13 13
 
14 14
     @Test
15 15
     public void findsAValueInAnArrayWithOneElement() {
16
-        List<Integer> listOfUnitLength = Collections.singletonList(6);
16
+        List<Character> listOfUnitLength = Collections.singletonList('6');
17 17
 
18
-        BinarySearch<Integer> search = new BinarySearch<>(listOfUnitLength);
18
+        BinarySearch<Character> search = new BinarySearch<>(listOfUnitLength);
19 19
 
20
-        assertEquals(0, search.indexOf(6));
20
+        assertEquals(0, search.indexOf('6'));
21 21
     }
22 22
 
23 23
     @Ignore("Remove to run test")
24 24
     @Test
25 25
     public void findsAValueInTheMiddleOfAnArray() {
26
-        List<Integer> sortedList = Collections.unmodifiableList(
27
-                Arrays.asList(1, 3, 4, 6, 8, 9, 11)
26
+        List<String> sortedList = Collections.unmodifiableList(
27
+                Arrays.asList("1", "3", "4", "6", "8", "9", "11")
28 28
         );
29 29
 
30
-        BinarySearch<Integer> search = new BinarySearch<>(sortedList);
30
+        BinarySearch<String> search = new BinarySearch<>(sortedList);
31 31
 
32
-        assertEquals(3, search.indexOf(6));
32
+        assertEquals(3, search.indexOf("6"));
33 33
     }
34 34
 
35 35
     @Ignore("Remove to run test")
@@ -83,13 +83,13 @@ public class BinarySearchTest {
83 83
     @Ignore("Remove to run test")
84 84
     @Test
85 85
     public void identifiesThatAValueIsNotIncludedInTheArray() {
86
-        List<Integer> sortedList = Collections.unmodifiableList(
87
-                Arrays.asList(1, 3, 4, 6, 8, 9, 11)
86
+        List<String> sortedList = Collections.unmodifiableList(
87
+                Arrays.asList("1", "3", "4", "6", "8", "9", "11")
88 88
         );
89 89
 
90
-        BinarySearch<Integer> search = new BinarySearch<>(sortedList);
90
+        BinarySearch<String> search = new BinarySearch<>(sortedList);
91 91
 
92
-        assertEquals(-1, search.indexOf(7));
92
+        assertEquals(-1, search.indexOf("7"));
93 93
     }
94 94
 
95 95
     @Ignore("Remove to run test")