Someone can explain to me what behavior has the comparable method To go putting objects to the queue with priority, according to I believe if it returns a negative the greater object is the one that is passed as a parameter. But in this exercise because it works the other way around, I get the answer that the method invokes, but it returns a minus 1. I do not understand why it works. If someone can explain it to me, thank you.
import java.util.PriorityQueue;
public class TestEx_c_jun14
{
public static void main(String args[])
{
PriorityQueue<Atomo> q=new PriorityQueue();
int n[]={100,2,10,5,50,20,25,3,1};
for(int i=0; i<n.length; i++)
q.offer(new Atomo(n[i]));
System.out.println(q.peek().toString());
}
}
class Atomo implements Comparable<Atomo>
{
int atributo;
public Atomo(int n) {
atributo=n;
}
public int getAtributo() { return atributo; }
public String toString() { return Integer.toString(atributo); }
public int compareTo(Atomo e) {
int c = 0;
if(e.getAtributo() % getAtributo() == 0)
c = +1;
else
c = -1;
return c;
}
}