當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java java.util.PriorityQueue.remove()用法及代碼示例


描述

這個remove(Object o)方法用於從此隊列中刪除指定元素的單個實例(如果存在)。

聲明

以下是聲明java.util.PriorityQueue.remove()方法。

public boolean remove(Object o)

參數

o─ 要從此隊列中移除的元素。

返回值

  • 如果此隊列因調用而更改,則方法調用將返回 'true'。

異常

NA

示例

下麵的例子展示了 java.util.PriorityQueue.remove() 的用法

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);

      // remove 7 from the queue
      boolean isremoved = prq.remove(7);

      System.out.println("Return value after remove:"+ isremoved);
      System.out.println("Priority queue values after remove:"+ prq);
   }
}

讓我們編譯並運行上麵的程序,這將產生以下結果。

Initial priority queue values are:[3, 4, 5, 6, 7, 8, 9]
Return value after remove:true
Priority queue values after remove:[3, 4, 5, 6, 9, 8]

相關用法


注:本文由純淨天空篩選整理自 java.util.PriorityQueue.remove() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。