2017-06-27 9 views
1

イム:コンパレータ

iは、現在のリストを持っている:

RG3 

PR1 

PR2 

RG4 

RG1 

RG2 

RGは、普通の人を指し、 PRは優先権を有する人物を指し、数字はターンを表す。 私が望むのは、優先順位の高い人が自分の順番でキューの先頭に行くとき以外は先入れ先出しを取得することです。そのリストには、私は、IVEが今まで行って何をHERESに次のような結果

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 

私は知っています私の問題は、コンパイラの方法ですが、私は望むキューを達成する方法を知らない。

私はtheresを比較する方法に関する多くの質問を知っていますが、私が見る唯一の答えは文字列を比較するときです。これは優先文字列とintを比較する必要があります。

答えて

0

だけ

if("PR".equals(cod_tipo_ficha)) { 
    ... 
} 

にcod_tipo_ficha == "PR" を変更するには、一般的なエラーです

+0

@amiqueを動作するはずです。私はそのようなバグを1日に5回しています。 – alexey28

関連する問題