Java中的PriorityQueue size()方法

爪哇。util。优先队列。size()方法用于获取PriorityQueue的大小或PriorityQueue中存在的元素数。

null

语法:

Priority_Queue.size()

参数: 此方法不接受任何参数。

返回值: 该方法返回PriorityQueue中存在的元素的大小或数量。

下面的程序演示了Java。util。优先队列。size()方法: 项目1:

// Java code to illustrate size()
import java.util.*;
public class PriorityQueueDemo {
public static void main(String args[])
{
// Creating an empty PriorityQueue
PriorityQueue<String> queue = new PriorityQueue<String>();
// Use add() method to add elements into the Queue
queue.add( "Welcome" );
queue.add( "To" );
queue.add( "Geeks" );
queue.add( "For" );
queue.add( "Geeks" );
// Displaying the PriorityQueue
System.out.println( "PriorityQueue: " + queue);
// Displaying the size of the PriorityQueue
System.out.println( "The size of the queue is: " + queue.size());
}
}


输出:

PriorityQueue: [For, Geeks, To, Welcome, Geeks]
The size of the queue is: 5

项目2:

// Java code to illustrate size()
import java.util.*;
public class PriorityQueueDemo {
public static void main(String args[])
{
// Creating an empty PriorityQueue
PriorityQueue<Integer> queue = new PriorityQueue<Integer>();
// Use add() method to add elements into the Queue
queue.add( 10 );
queue.add( 15 );
queue.add( 30 );
queue.add( 20 );
queue.add( 5 );
queue.add( 18 );
// Displaying the PriorityQueue
System.out.println( "PriorityQueue: " + queue);
// Displaying the size of the PriorityQueue
System.out.println( "The size of the queue is: " + queue.size());
}
}


输出:

PriorityQueue: [5, 10, 18, 20, 15, 30]
The size of the queue is: 6

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享