Java.util.ArrayDeque.descendingIterator()方法實例
java.util.ArrayDeque.descendingIterator() 方法返回一個迭代器在此deque 隊列以逆向順序的元素。
聲明
以下是java.util.ArrayDeque.descendingIterator()方法的聲明
public Iterator<E> descendingIterator()
參數
-
NA
返回值
此方法返回一個迭代器在此deque隊列的順序相反的元素。
異常
-
NA
例子
下麵的示例演示java.util.ArrayDeque.descendingIterator()方法的用法。
package com.yiibai; import java.util.ArrayDeque; import java.util.Deque; import java.util.Iterator; public class ArrayDequeDemo { public static void main(String[] args) { // create an empty array deque with an initial capacity Deque<Integer> deque = new ArrayDeque<Integer>(8); // use add() method to add elements in the deque deque.add(15); deque.add(22); deque.add(25); deque.add(20); //iterator() is used to print all the elements //next() returns the next element on each iteration System.out.println("printing elements using iterator:"); for(Iterator itr = deque.iterator();itr.hasNext();) { System.out.println(itr.next()); } //descendingIterator() is used to print the elements in reverse order //next() returns the next element on each iteration System.out.println("printing elements in reverse order:"); for(Iterator descItr = deque.descendingIterator();descItr.hasNext();) { System.out.println(descItr.next()); } } }
讓我們來編譯和運行上麵的程序,這將產生以下結果:
printing elements using iterator: 15 22 25 20 printing elements in reverse order: 20 25 22 15