#Object sequential searching using compareTo()

27 messages · Page 1 of 1 (latest)

late pebble
#

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;
    }```
tepid magnetBOT
#

This post has been reserved for your question.

Hey @late pebble! 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.

late pebble
#

Object sequential searching using compareTo()

#

why isn't it: <T Comparable<T>>

late pebble
#

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;
    }
spiral flicker
#

@late pebble
Interfaces do not implement other interfaces, they can tho be extended by other interfaces, thats why you see the extends keyword there

And T extends Comparable<T> is used since Comparable is a generic interface, it means any dataype T that implements the Comparable interface to enable comparing of your wished objects

tepid magnetBOT
#

💤 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.

late pebble
#

TIA

spiral flicker
tepid magnetBOT
#

💤 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.

late pebble
barren mantle
#

@late pebble T can be extended to any interface that extends the Comparable interface

tame hawk
#

extends in a generic is not the same as extends in a class declaration

#

extends and super constraints in generics are just specifying the top and bottom constraints, what types T needs to be a subtype or supertype of

#

both oppa and deathlyonion are incorrect here unfortunately

#

extends is used in 3 contexts

  • class extends class
  • interface extends interface
  • generic T extends bound
    they all have the same connotation of subtyping, but they do not mean the exact same thing
civic tulip
#

T extends InterfaceB & InterfaceC is valid syntax

tame hawk
tame hawk
tame hawk
#

though to be pedantic, you want a single noun there, extends U is more of a specific descriptor