boolean add(E e)
描述 (Description)
add(E e)方法用于将指定的元素插入优先级队列。
声明 (Declaration)
以下是java.util.PriorityQueue.add()方法的声明。
public boolean add(E e)
参数 (Parameters)
e - 要添加的元素。
返回值 (Return Value)
方法调用返回true(由Collection.add(E)指定)
异常 (Exception)
ClassCastException - 如果无法根据优先级队列的顺序将指定的元素与当前在此优先级队列中的元素进行比较, ClassCastException抛出此异常。
NullPointerException - 如果指定的元素为null,则抛出此异常。
例子 (Example)
以下示例显示了java.util.PriorityQueue.add()的用法
package com.iowiki;
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 = 0; i < 10; i++ ) {
prq.add (new Integer (i)) ;
}
System.out.println("Priority queue values are: " + prq) ;
}
}
让我们编译并运行上面的程序,这将产生以下结果。
Priority queue values are: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]