📜  Java中的双端迭代器()方法

📅  最后修改于: 2022-05-13 01:54:49.235000             🧑  作者: Mango

Java中的双端迭代器()方法

Deque 接口iterator()方法以适当的顺序返回此双端队列中元素的迭代器。元素将按从第一个(头)到最后一个(尾)的顺序返回。返回的迭代器是一个“弱一致”的迭代器。

句法:

Iterator iterator()

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

返回:此方法以正确的顺序返回此双端队列中元素的迭代器。

下面的程序说明了 Deque 的iterator()方法:

程序 1:ArrayDeque的帮助下。

// Java Program Demonstrate iterator()
// method of Deque
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of Deque
        Deque DQ
            = new ArrayDeque();
  
        // Add numbers to end of Deque
        DQ.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
  
        // Call iterator() method of Deque
        Iterator iteratorVals = DQ.iterator();
  
        // Print elements of iterator
        // created from Deque
        System.out.println("The iterator values"
                           + " of Deque are:");
  
        // prints the elements using an iterator
        while (iteratorVals.hasNext()) {
            System.out.println(iteratorVals.next());
        }
    }
}
输出:
The iterator values of Deque are:
7855642
35658786
5278367
74381793

程序 2:LinkedList的帮助下。

// Java Program Demonstrate iterator()
// method of Deque
import java.util.*;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of Deque
        Deque DQ
            = new LinkedList();
  
        // Add numbers to end of Deque
        DQ.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
  
        // Call iterator() method of Deque
        Iterator iteratorVals = DQ.iterator();
  
        // Print elements of iterator
        // created from Deque
        System.out.println("The iterator values"
                           + " of Deque are:");
  
        // prints the elements using an iterator
        while (iteratorVals.hasNext()) {
            System.out.println(iteratorVals.next());
        }
    }
}
输出:
The iterator values of Deque are:
7855642
35658786
5278367
74381793

程序 3:LinkedBlockingDeque的帮助下。

// Java Program Demonstrate iterator()
// method of Deque
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of Deque
        Deque DQ
            = new LinkedBlockingDeque();
  
        // Add numbers to end of Deque
        DQ.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
  
        // Call iterator() method of Deque
        Iterator iteratorVals = DQ.iterator();
  
        // Print elements of iterator
        // created from Deque
        System.out.println("The iterator values"
                           + " of Deque are:");
  
        // prints the elements using an iterator
        while (iteratorVals.hasNext()) {
            System.out.println(iteratorVals.next());
        }
    }
}
输出:
The iterator values of Deque are:
7855642
35658786
5278367
74381793

程序 4:ConcurrentLinkedDeque的帮助下。

// Java Program Demonstrate iterator()
// method of Deque
import java.util.*;
import java.util.concurrent.ConcurrentLinkedDeque;
  
public class GFG {
    public static void main(String[] args)
        throws IllegalStateException
    {
  
        // create object of Deque
        Deque DQ
            = new ConcurrentLinkedDeque();
  
        // Add numbers to end of Deque
        DQ.add(7855642);
        DQ.add(35658786);
        DQ.add(5278367);
        DQ.add(74381793);
  
        // Call iterator() method of Deque
        Iterator iteratorVals = DQ.iterator();
  
        // Print elements of iterator
        // created from Deque
        System.out.println("The iterator values"
                           + " of Deque are:");
  
        // prints the elements using an iterator
        while (iteratorVals.hasNext()) {
            System.out.println(iteratorVals.next());
        }
    }
}
输出:
The iterator values of Deque are:
7855642
35658786
5278367
74381793

参考: https: Java/util/Deque.html#iterator–