當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Deque iterator()用法及代碼示例


雙端隊列接口的getLast()方法以適當的順序在此雙端隊列中的元素上返回迭代器。元素將按照從頭(頭)到後(尾)的順序返回。返回的迭代器是一個弱一致性迭代器。

用法:

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<Integer> DQ 
            = new ArrayDeque<Integer>(); 
  
        // 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<Integer> DQ 
            = new LinkedList<Integer>(); 
  
        // 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<Integer> DQ 
            = new LinkedBlockingDeque<Integer>(); 
  
        // 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<Integer> DQ 
            = new ConcurrentLinkedDeque<Integer>(); 
  
        // 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://docs.oracle.com/javase/8/docs/api/java/util/Deque.html#iterator-



相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 Deque iterator() method in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。