PriorityQueue类的iterator()方法 (PriorityQueue Class iterator() method)
iterator() method is available in java.util package.
iterator()方法在java.util包中可用。
iterator() method is used to iterates the elements by using iterator() method in this PriorityQueue.
iterator()方法用于在此PriorityQueue中使用iterator()方法来迭代元素。
iterator() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
iterator()方法是一个非静态方法,只能通过类对象访问,如果尝试使用类名称访问该方法,则会收到错误消息。
iterator() method does not throw an exception at the time of returning the Iterator.
返回迭代器时,iterator()方法不会引发异常。
Syntax:
句法:
public Iterator iterator();
Parameter(s):
参数:
It does not accept any parameter.
它不接受任何参数。
Return value:
返回值:
The return type of the method is Iterator, it returns an Iterator object is to iterate the PriorityQueue elements.
该方法的返回类型是Iterator ,它返回一个Iterator对象是要迭代PriorityQueue元素。
Example:
例:
// Java program to demonstrate the example
// of Iterator iterator() method of
// PriorityQueue
import java.util.*;
public class IteratorOfPriorityQueue {
public static void main(String args[]) {
// Instantiate PriorityQueue
PriorityQueue < String > pq = new PriorityQueue < String > ();
// By using add() method is add
// the given element into priority
// queue
pq.add("C");
pq.add("C++");
pq.add("JAVA");
pq.add("PHP");
pq.add("ANDROID");
// Display PriorityQueue
System.out.println("PriorityQueue: " + pq);
// By using iterator() method is to
// iterate the PriorityQueue elements
for (Iterator iter = pq.iterator(); iter.hasNext();)
System.out.println(iter.next());
}
}
Output
输出量
PriorityQueue: [ANDROID, C, JAVA, PHP, C++]
ANDROID
C
JAVA
PHP
C++
翻译自: https://www.includehelp.com/java/priorityqueue-iterator-method-with-example.aspx