java - Generics and compareTo() method -
i trying make skiplist , have method takes generic data type:
public void add(e key, integer value) { node<e> p; p = find(key); } which takes here:
public node<e> find(e key) { //start @ head node<e> p = head; while (true) { while ( (p.getright().getkey() != node.posinf) && (p.getright().getkey().compareto(key) <= 0 )) { p.setright(p.getright()); } //more stuff down here } } the problem on compareto() method. says compareto() method undefined type e. in eclipse wants me add 2 typecasts this:
((string) p.getright().getkey().compareto((string) key) <= 0 )
why want string? data type anything. tried doing typecast of e instead eclipse wants change string. appreciated.
you haven't shown how e defined, error message indicates didn't place upper bound of comparable<e> on declaration of e.
you can accomplish on class:
public class skiplist<e extends comparable<e>> this allow call compareto on key variable of type e.
as why eclipse suggesting casting string, looks eclipse guessing best change make make compile. may have guessed string because it's comparable<string>. in case, it's wrong, because e isn't string. solution here different, stated above: restrict e comparable<e>.
Comments
Post a Comment