java - Errors using the Comparator interface -
i trying write simple application priority queues. getting following errors --" error: leastpriority not abstract , not override abstract method compare(integer,integer) in comparator" , "error: incompatible types comparator cmp = new leastpriority(); "
can point out problem code.
my code :
class leastpriority implements comparator<integer> { public int compare(reservation x, reservation y){ if(x.getpriority() > y.getpriority()){ return -1; } if(x.getpriority() < y.getpriority()){ return +1; } return 0; } } public class prioqueue{ public static void main(string args[]){ comparator<reservation> cmp = new leastpriority(); priorityqueue<reservation> queue = new priorityqueue<reservation>(10,cmp); queue.add(new reservation(1,"andy",10)); queue.add(new reservation(1,"peter",1)); queue.add(new reservation(1,"john",4)); while(true){ reservation r = queue.poll(); if(r==null){ break; } system.out.println(r.getname()); } } }
your type parameter comparator<t>
, compare(t o1, t o2)
method's parameters not match. since in interface same, need give identical types them.
change this:
class leastpriority implements comparator<integer>
to:
class leastpriority implements comparator<reservation>
Comments
Post a Comment