Object sequential searching using compareTo()

I have this simple method to search for an object that uses Comparable interface. But I'm wondering, why does my parameter should use extends instead of implements? Doesn't this break the law where an object can only extend ONE class, but is able to implement multiple interfaces? So why doesn't implements work here?
public static <T extends Comparable<T>> int sequentialSearch(T[] list, T key) {
for (int i = 0; i < list.length; i++) {
if (list[i].compareTo(key) == 0)
return i;
}
return -1;
}
public static <T extends Comparable<T>> int sequentialSearch(T[] list, T key) {
for (int i = 0; i < list.length; i++) {
if (list[i].compareTo(key) == 0)
return i;
}
return -1;
}
12 Replies
JavaBot
JavaBot2y ago
This post has been reserved for your question.
Hey @circle! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
circle
circleOP2y ago
why isn't it: <T Comparable<T>> Another method I can use is using raw implementation of casting the to be compared object to Comparable interface. But this still means it was casted to a class(?) instead of the interface.
public static int sequentialSearch(Object[] x, Object key) {
for (int i = 0; i < x.length; i++) {
if ( ((Comparable) x[i]).compareTo(key) == 0 )
return i;
}
return -1;
}
public static int sequentialSearch(Object[] x, Object key) {
for (int i = 0; i < x.length; i++) {
if ( ((Comparable) x[i]).compareTo(key) == 0 )
return i;
}
return -1;
}
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JavaBot
JavaBot2y ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one.
circle
circleOP2y ago
Sorry for the late reply, does that mean T as the parameter data type is already an interface? TIA
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JavaBot
JavaBot2y ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one.
circle
circleOP2y ago
So T can be extended into any interface? Even though interface is not a class?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Carter
Carter2y ago
T extends InterfaceB & InterfaceC is valid syntax
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
JavaBot
JavaBot2y ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one.
Want results from more Discord servers?
Add your server