-
-
Notifications
You must be signed in to change notification settings - Fork 735
binary-search: use generics #868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
This exercise introduces [generics](https://docs.oracle.com/javase/tutorial/java/generics/index.html). | ||
To make the tests pass you need to construct your class such that it accepts any type of input, e.g. `Integer` or `String`. | ||
|
||
Generics are useful because they allow you to write more general and reusable code. | ||
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. | ||
By using them you can construct a `List` containing `Integers` or a list containing `Strings` or any other type. | ||
|
||
There are a few constraints on the types used in generics. | ||
One of them is that once you've constructed a `List` containing `Integers`, you can't put `Strings` into it. | ||
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. | ||
|
||
For example you could construct a list of `Integers`: | ||
|
||
`List<Integer> someList = new LinkedList();` | ||
|
||
Now `someList` can only contain `Integers`. You could also do: | ||
|
||
`List<String> someOtherList = new LinkedList()` | ||
|
||
|
||
Now `someOtherList` can only contain `Strings`. | ||
|
||
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`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be good to add a reminder that every primitive type has a corresponding reference type! |
||
|
||
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. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,23 +13,23 @@ public class BinarySearchTest { | |
|
||
@Test | ||
public void findsAValueInAnArrayWithOneElement() { | ||
List<Integer> listOfUnitLength = Collections.singletonList(6); | ||
List<Character> listOfUnitLength = Collections.singletonList('6'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same question as for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really. I've tried to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nah, I think it's good as-is! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will change it to cycle through There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As I was changing it I remembered why I didn't do that. Some of them can't use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This sounds like a good compromise to me! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, will do that :) Will do that for the others too, try to cycle through all three but skip There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sadly, as it turns out There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That sounds ideal, yep! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great, will do that then :) |
||
|
||
BinarySearch<Integer> search = new BinarySearch<>(listOfUnitLength); | ||
BinarySearch<Character> search = new BinarySearch<>(listOfUnitLength); | ||
|
||
assertEquals(0, search.indexOf(6)); | ||
assertEquals(0, search.indexOf('6')); | ||
} | ||
|
||
@Ignore("Remove to run test") | ||
@Test | ||
public void findsAValueInTheMiddleOfAnArray() { | ||
List<Integer> sortedList = Collections.unmodifiableList( | ||
Arrays.asList(1, 3, 4, 6, 8, 9, 11) | ||
List<String> sortedList = Collections.unmodifiableList( | ||
Arrays.asList("1", "3", "4", "6", "8", "9", "11") | ||
); | ||
|
||
BinarySearch<Integer> search = new BinarySearch<>(sortedList); | ||
BinarySearch<String> search = new BinarySearch<>(sortedList); | ||
|
||
assertEquals(3, search.indexOf(6)); | ||
assertEquals(3, search.indexOf("6")); | ||
} | ||
|
||
@Ignore("Remove to run test") | ||
|
@@ -83,13 +83,13 @@ public void findsAValueInAnArrayOfEvenLength() { | |
@Ignore("Remove to run test") | ||
@Test | ||
public void identifiesThatAValueIsNotIncludedInTheArray() { | ||
List<Integer> sortedList = Collections.unmodifiableList( | ||
Arrays.asList(1, 3, 4, 6, 8, 9, 11) | ||
List<String> sortedList = Collections.unmodifiableList( | ||
Arrays.asList("1", "3", "4", "6", "8", "9", "11") | ||
); | ||
|
||
BinarySearch<Integer> search = new BinarySearch<>(sortedList); | ||
BinarySearch<String> search = new BinarySearch<>(sortedList); | ||
|
||
assertEquals(-1, search.indexOf(7)); | ||
assertEquals(-1, search.indexOf("7")); | ||
} | ||
|
||
@Ignore("Remove to run test") | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should use
LinkedList<>();
here.