java.util.PriorityQueue.poll() 方法



描述

poll() 方法用於檢索並刪除此佇列的頭部,如果此佇列為空則返回 null。

宣告

以下是 java.util.PriorityQueue.poll() 方法的宣告。

public E poll()							 

引數

返回值

  • 方法呼叫返回佇列的頭部,如果佇列為空則返回 null。

異常

示例

以下示例顯示了 java.util.PriorityQueue.poll() 的用法

package com.tutorialspoint;

import java.util.*;

public class PriorityQueueDemo {
   public static void main(String args[]) {

      // create priority queue
      PriorityQueue < Integer >  prq = new PriorityQueue < Integer > (); 

      // insert values in the queue
      for ( int i = 3; i  <  10; i++ ) {  
         prq.add (new Integer (i)) ; 
      }

      System.out.println("Initial priority queue values are: "+ prq);

      // get the head from the queue
      Integer head = prq.poll();

      System.out.println("Head of the queue is: "+ head);
      System.out.println("Priority queue values after poll: "+ prq);
   }
}

讓我們編譯並執行以上程式,這將產生以下結果。

Initial priority queue values are: [3, 4, 5, 6, 7, 8, 9]
Head of the queue is: 3
Priority queue values after poll: [4, 6, 5, 9, 7, 8]
java_util_priorityqueue.htm
廣告

© . All rights reserved.