1
我試着使用的PriorityQueue的倍率比較方法,我想實現以下目標:比較的PriorityQueue的
我有當前列表:
RG3
PR1
PR2
RG4
RG1
RG2
的RG指的是普通的人PR是指有優先權的人,數字代表輪流。 我想要的是獲得先進先出的順序,除非什麼時候是輪到隊列頂部的優先人選。所以在列表中,我想下面的結果
PR1
PR2
RG1
RG2
RG3
RG4
繼承人什麼香港專業教育學院做了到現在爲止:
Queue<Ficha> cola = new PriorityQueue<>(6, idComparator);
while (!list.isEmpty()) //this list is the unsorted list.
{
aux = list.remove(0);
cola.add(aux); // adds it to the priority queue
}
while(!cola.isEmpty())
{
aux = cola.poll();
System.out.println(aux.getCod_priority()+aux.getTurn()); // this shows me the order of the queue
}
}
public static Comparator<Ficha> idComparator = new Comparator<Ficha>()
{
@Override
public int compare(Ficha f1, Ficha f2) {
return (int) ((f1.getTurn()+prioridad(f1.getCod_priority())) - (f2.getTurn()+prioridad(f2.getCod_priority())));
}
};
private static long prioridad(String cod_priority) // this method i use it to give the cod_priority a int value to compare
{
if(cod_tipo_ficha=="PR")
{
return 10000;
}
else
{
return 1;
}
}
,當我運行它,我得到以下順序:
PR1
RG1
RG2
PR2
RG3
RG4
我知道我的問題是比較方法,但我不知道如何實現我想要的隊列。
我知道這裏有很多關於如何比較的問題,但我看到的唯一答案是當你比較字符串時。而這一個我需要比較優先級字符串和int。
@amique這是常見的錯誤。我每天做5個這樣的錯誤。 – alexey28