java - Error . operator < cannot be applied -
java - Error . operator < cannot be applied -
in line 11 there error .
operator < cannot applied java.lang.object,java.lang.object
i don't quite understand 1 time again i'm new generics in java . bear me please.
public void sortbykey() { linkedlist temp = new linkedlist(); linkedlist 3rd = new linkedlist(); temp=head; if(temp!=null) { while(temp.next!=null) { if (temp.key < temp.next.key) { third.item = temp.key; temp.key = temp.next.key; temp.next.key = third.item; } else temp = temp.next; } } }
simply, operator can applied on numeric types, such int
, byte
, double
, float
, etc , boxing classes (such integer
, byte
, etc.).
it doesn't work combined generics, because compiler doesn't know exact types of operands. when working generics, these types set @ runtime, after type erasure has occurred.
a possible solution create class of key
property implement comparable<t>
interface , able compare with:
temp.key.compareto(temp.next.key) > 0
java generics compiler-errors
Comments
Post a Comment