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


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


描述

這個size()方法用於返回此集合中的元素數。

聲明

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

public int size()

參數

NA

返回值

  • 方法調用返回此集合中的元素數

異常

NA

示例

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

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("Size of the queue is:"+ prq.size());
      System.out.println("Priority queue values are:"+ prq);
   }
}

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

Size of the queue is:7
Priority queue values are:[3, 4, 5, 6, 7, 8, 9]

相關用法


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