samedi 25 avril 2015

Greater Than Operator Undefined on Generic Type, Despite extending and implementing Comparable


I searched and found several instances of similar problems but the obvious solutions have already been implemented, so I'm at a bit of a loss. FYI: This is a homework assignment, if it matters.

public class Entry<K extends Comparable<K>, V> implements 
    Comparable<Entry<K, V>> {

    protected K key;
    protected V value;

    protected Entry(K k, V v) {
        key = k;
        value = v;
    }

    public K key() {
        return key;
    }

    public V value() {
        return value;
    }

    // override toString method
    // toString method should print the entry as:
    // <key,value>
    @Override
    public String toString() {
        return "<>" + key.toString() + ", " + value.toString();
    }

    public int compareTo(Entry<K, V> other) {
        if (this.key > other.key)
            return 1;
        else if (this.key == other.key)
            return 0;
        else
            return -1;
    }
}

The error that I'm getting is:

The operator > is undefined for the argument type(s) K, K"

in the first line of the compareTo method.


Aucun commentaire:

Enregistrer un commentaire