爪哇。util。优先队列。iterator()方法用于返回与优先级队列相同元素的迭代器。元素按随机顺序从队列中的元素返回。
null
语法:
Iterator iterate_value = Priority_Queue.iterator();
参数: 该函数不接受任何参数。
返回值: 该方法迭代队列的元素并返回值(迭代器)。
下面的程序演示了Java。util。优先队列。迭代器()方法: 项目1:
// Java code to illustrate iterator() 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( "4" ); queue.add( "Geeks" ); // Displaying the PriorityQueue System.out.println( "PriorityQueue: " + queue); // Creating an iterator Iterator value = queue.iterator(); // Displaying the values after iterating through the queue System.out.println( "The iterator values are: " ); while (value.hasNext()) { System.out.println(value.next()); } } } |
输出:
PriorityQueue: [4, Geeks, To, Welcome, Geeks] The iterator values are: 4 Geeks To Welcome Geeks
项目2:
// Java code to illustrate iterator() 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 ); // Displaying the PriorityQueue System.out.println( "PriorityQueue: " + queue); // Creating an iterator Iterator value = queue.iterator(); // Displaying the values after iterating through the queue System.out.println( "The iterator values are: " ); while (value.hasNext()) { System.out.println(value.next()); } } } |
输出:
PriorityQueue: [5, 10, 30, 20, 15] The iterator values are: 5 10 30 20 15
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END