-
-
Notifications
You must be signed in to change notification settings - Fork 709
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
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`. | ||
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). | ||
|
||
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. |
10 changes: 3 additions & 7 deletions
10
exercises/binary-search/.meta/src/reference/java/BinarySearch.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Same question as for
custom-set
; is there a pattern for the generic types being used now?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.
Not really. I've tried to use
Integer
,Character
andString
and to make sure I've got a couple of tests using each, but I've not got a strict pattern as I didn't think it mattered too much. I could make it a more strict pattern if you think that would be useful?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.
Nah, I think it's good as-is!
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.
Will change it to cycle through
Character
,String
,Integer
as it's probably best to be consistent :)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.
As I was changing it I remembered why I didn't do that. Some of them can't use
Character
because they use higher numbers, e.g.11
... So it's only possible to useCharacter
for some of the earlier tests, at least for this exercise. I could make the rest cycle throughString
andInteger
?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.
This sounds like a good compromise to me!
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.
Okay, will do that :) Will do that for the others too, try to cycle through all three but skip
Character
if not possible :)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.
Sadly, as it turns out
String
causes problems as well since it will sort it lexicographically and we're using numbers in all our test cases... So should I instead try to cycle through all three, skipChracter
if not possible and also skipString
if not possible? And make sure thatCharacter
andString
are used at least once?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.
That sounds ideal, yep!
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.
Great, will do that then :)