1

i'm trying to run this but i can't compare that type with < or > it gets this error: The operator > is undefined for the argument type(s) T, T, anytips on how to fix it????????????????????????????

public class GenericElement<T extends Comparable<T>> implements Comparable<GenericElement<T>>

{

private GenericElement next;
private T value; 

public GenericElement(T value, GenericElement next) {
    this.value = value;
    this.next = next;
}

public GenericElement getNext() {   
    return next;
}

public void setNext(GenericElement next) {
    this.next = next;
}

public T getValue() {
    return value;
}

public void setValue(T value) {
    this.value = value;
}


@Override
public int compareTo(GenericElement<T> o) {
    // TODO Auto-generated method stub
    if(o.getValue()>this.getValue()) //i get error here The operator > is undefined for the argument type(s) T, T {
        return -1;
    }
    else if(o.getValue()==this.getValue()) {
        return 0;
    }
    else {
        return 1;
    }
}

}

  • Yes, you have a loose `>` after `o.getValue()` which shouldn't be there. Remove it. – rzwitserloot Jun 03 '22 at 23:21
  • 1
    Java doesn't support operator overloading by the user. You can't use `>` and such with implementations of `Comparable`. You have to call the `compareTo` method and inspect the result. – Slaw Jun 04 '22 at 00:00

0 Answers0