0

我試圖在Java中實現PriorityQueue。我有PrinterQueue類,其中包括用於測試的PriorityQueue和TestPrinterQueue類。 PrintJob是此隊列中的類型。構造函數是私有的,所以我使用反射來實現對象的實例。Java中的PriorityQueue類的添加操作

在運行時發生問題,我無法將多個對象添加到隊列中。隊列的大小始終相同。沒有編譯錯誤。所有的代碼都在下面。謝謝...

PrinterQueue class: 

import java.util.Comparator; 
import java.util.PriorityQueue; 

public class PrinterQueue{ 
    private static PrinterQueue queue; 
    PriorityQueue<PrintJob> pqExample; 
    Comparator<PrintJob> Comparator; 
    int iQueueSize = 50; 

    private PrinterQueue(){ 
     Comparator = new Priority(); 
     pqExample = new PriorityQueue<PrintJob>(iQueueSize,Comparator); 
    } 
    public static PrinterQueue getQueue(){ 
     if (queue==null) queue = new PrinterQueue(); 
    return queue;} 

    public void addJob (PrintJob job) { 
     queue = new PrinterQueue(); 
     queue.pqExample.add(job); 
    // Use the job’s getPriority method to check its priority. PRIORITY CLASSINDA KULLANILDI. 
    System.out.println("Job " + job.getName() + " is added to the printer queue" + " Size: " + queue.pqExample.size()); 
    Print(); 
    } 
    public boolean isEmpty(){ 
     if(queue.pqExample.isEmpty()) 
      return true; 
     else 
      return false; 
    } 
    public PrintJob removeJob(){ 
     //TODO –remove the highest priority job from the queue 
     PrintJob job = queue.pqExample.remove(); 
     System.out.println("Job "+ job.getName()+ "has been printed"); 
     return job; 

    } 
    public boolean isFull(){ 
     if (queue.pqExample.size() == iQueueSize) 
      return true; 
     else 
      return false; 
    } 
    private void Print() { 

     PriorityQueue<PrintJob> pqTemp; 
     pqTemp = queue.pqExample; 

     while (pqTemp.size() != 0) 
     { 
      System.out.println(queue.pqExample.element()); 
      System.out.println("//"); 
      pqTemp.remove(); 
     } 
    } 
} 

TestPrinterQueue類:

import java.lang.reflect.Constructor; 
import java.util.Scanner; 

public class TestPrinterQueue { 

    public static void main (String[] args) throws Exception { 

     Constructor<PrinterQueue> constructor = PrinterQueue.class.getDeclaredConstructor(new Class[0]); 
     constructor.setAccessible(true); 

     PrinterQueue pqTest = constructor.newInstance(new Object[0]); 

     @SuppressWarnings("resource") 
     Scanner scUserInput = new Scanner(System.in); 

     while (true) 
     { 
      PrintJob pjWillbeAdded = new PrintJob(); 

      System.out.println("Enter the priority:"); 
      pjWillbeAdded.setPriority(scUserInput.nextInt()); 
      System.out.println("Enter the JobName:"); 
      pjWillbeAdded.setName(scUserInput.next()); 
      System.out.println("Enter the Number of Pages in PrintJob:"); 
      pjWillbeAdded.setiNoPJ(scUserInput.nextInt()); 

      pqTest.addJob(pjWillbeAdded); 

      System.out.println("**** TEST SIZE : " + pqTest.pqExample.size() + "*****"); 
     } 
    } 
} 

我希望這個信息就足以說明我的問題。

+1

ouch,不知道從哪裏開始,但是,每次向隊列中添加某些內容時,都會通過調用Print()將其清空。這可能是你看到的恆定大小的共鳴。 –

+0

男人,非常感謝你,現在它運作良好!但我仍然不明白這個機制,因爲在Print函數中,我不會從主隊列中刪除元素。我將它們從臨時隊列中移出,這與另一個相等。我將再次檢查它。 – skynyrd

+1

您並未創建臨時隊列,只是對原始的另一個引用。 –

回答

1

在您的addJob()方法中,您每次都將隊列分配給新的PrinterQueue對象。將其更改爲「queue = getQueue()」應該可以解決這個問題。

+0

謝謝,我興奮地做到了,但它不起作用。尺寸仍然是一樣..試圖找出.. – skynyrd

+0

謝謝你的回答,但我發現在不同的地方發生的錯誤。這是由打印功能推理。 – skynyrd