Skip to content

Commit b1a0b1d

Browse files
authored
Merge pull request #868 from FridaTveit/BinarySearchUseGenerics
binary-search: use generics
2 parents 34c804b + 8b9a7a2 commit b1a0b1d

File tree

4 files changed

+51
-30
lines changed

4 files changed

+51
-30
lines changed

config.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -654,18 +654,6 @@
654654
"unlocked_by": "word-count",
655655
"uuid": "76d28d97-75d3-47eb-bb77-3d347b76f1b6"
656656
},
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-
},
669657
{
670658
"core": false,
671659
"difficulty": 6,
@@ -764,6 +752,18 @@
764752
"unlocked_by": "scrabble-score",
765753
"uuid": "4b3f7771-c642-4278-a3d9-2fb958f26361"
766754
},
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+
},
767767
{
768768
"core": false,
769769
"difficulty": 6,
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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.

exercises/binary-search/.meta/src/reference/java/BinarySearch.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11

22
import java.util.List;
33

4-
public class BinarySearch<T extends Comparable<T>> {
4+
class BinarySearch<T extends Comparable<T>> {
55

66
private List<T> array;
77
private int arraySize;
88

9-
public BinarySearch(List<T> array) {
9+
BinarySearch(List<T> array) {
1010
this.array = array;
1111
this.arraySize = array.size();
1212
}
1313

14-
public int indexOf(T value) {
14+
int indexOf(T value) {
1515
return search(value);
1616
}
1717

18-
public List<T> getArray() {
19-
return array;
20-
}
21-
2218
private int search(T value) {
2319
int left = 0;
2420
int right = this.arraySize - 1;

exercises/binary-search/src/test/java/BinarySearchTest.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@ public class BinarySearchTest {
1313

1414
@Test
1515
public void findsAValueInAnArrayWithOneElement() {
16-
List<Integer> listOfUnitLength = Collections.singletonList(6);
16+
List<Character> listOfUnitLength = Collections.singletonList('6');
1717

18-
BinarySearch<Integer> search = new BinarySearch<>(listOfUnitLength);
18+
BinarySearch<Character> search = new BinarySearch<>(listOfUnitLength);
1919

20-
assertEquals(0, search.indexOf(6));
20+
assertEquals(0, search.indexOf('6'));
2121
}
2222

2323
@Ignore("Remove to run test")
2424
@Test
2525
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")
2828
);
2929

30-
BinarySearch<Integer> search = new BinarySearch<>(sortedList);
30+
BinarySearch<String> search = new BinarySearch<>(sortedList);
3131

32-
assertEquals(3, search.indexOf(6));
32+
assertEquals(3, search.indexOf("6"));
3333
}
3434

3535
@Ignore("Remove to run test")
@@ -83,13 +83,13 @@ public void findsAValueInAnArrayOfEvenLength() {
8383
@Ignore("Remove to run test")
8484
@Test
8585
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")
8888
);
8989

90-
BinarySearch<Integer> search = new BinarySearch<>(sortedList);
90+
BinarySearch<String> search = new BinarySearch<>(sortedList);
9191

92-
assertEquals(-1, search.indexOf(7));
92+
assertEquals(-1, search.indexOf("7"));
9393
}
9494

9595
@Ignore("Remove to run test")

0 commit comments

Comments
 (0)