java iterator
ArrayDeque类iterator()方法 (ArrayDeque Class iterator() method)
iterator() Method is available in java.lang package.
iterator()方法在java.lang包中可用。
iterator() Method is used to return an iterator over the deque elements.
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<T> iterator();
Parameter(s):
参数:
It does not accept any parameter.
它不接受任何参数。
Return value:
返回值:
The return type of the method is Iterator<T>, it returns an iterator over the deque elements.
该方法的返回类型为Iterator <T> ,它在双端队列元素上返回一个迭代器。
Example:
例:
// Java program to demonstrate the example
// of Iterator<T> iterator() method of ArrayDeque
import java.util.*;
public class IteratorOfArrayDeque {
public static void main(String[] args) {
// Creating an ArrayDeque with initial capacity of
// storing elements
Deque < String > d_queue = new ArrayDeque < String > (10);
// By using add() method to add elements
// in ArrayDeque
d_queue.add("C");
d_queue.add("C++");
d_queue.add("Java");
d_queue.add("Php");
d_queue.add("DotNet");
System.out.println();
// Display Deque Elements
System.out.println("d_queue before iterator() : ");
System.out.println("ArrayDeque Elements = " + d_queue);
System.out.println();
// By using iterator() method to iterate the
// elements in same order as ArrayDeque
System.out.println("d_queue after iterator(): ");
// Display Deque Elements
for (Iterator iter = d_queue.iterator(); iter.hasNext();)
System.out.println("Deque Elements : " + iter.next());
}
}
Output
输出量
d_queue before iterator() :
ArrayDeque Elements = [C, C++, Java, Php, DotNet]d_queue after iterator():
Deque Elements : C
Deque Elements : C++
Deque Elements : Java
Deque Elements : Php
Deque Elements : DotNet
翻译自: https://www.includehelp.com/java/arraydeque-iterator-method-with-example.aspx
java iterator