Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 7th, 2012  |  syntax: None  |  size: 0.70 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Unexpected behavior with PriorityQueue remove: Why isn't compareTo used?
  2. PriorityQueue<OwnClass> pq=new PriorityQueue<OwnClass>();
  3. OwnClass a=new OwnClass(1);
  4. OwnClass b=new OwnClass(2);
  5. OwnClass c=new OwnClass(3);
  6. pq.add(a);
  7. pq.add(b);
  8. pq.add(c);
  9. System.out.println("head:"+pq.peek());
  10. pq.remove(new OwnClass(1));
  11. System.out.println(pq.peek());
  12.        
  13. class OwnClass implements Comparable{
  14.  
  15.     int x;
  16.  
  17.     public OwnClass(int x){
  18.         this.x=x;
  19.     }
  20.  
  21.     public int compareTo(Object arg0) {
  22.  
  23.         OwnClass a=(OwnClass) arg0;
  24.         if(a.x>this.x)
  25.             return -1;
  26.         if(a.x<x)
  27.             return 1;
  28.         return 0;
  29.     }
  30.  
  31.     public String toString(){
  32.         return ""+x;        
  33.     }
  34. }