Java ArrayDeque descendingIterator()方法
java.util.ArrayDeque.descendingIterator() 此方法以相反的顺序返回此双端队列中的元素的迭代器。
1 语法
public Iterator<E> descendingIterator()
2 参数
无
3 返回值
此方法以相反的顺序在此双端队列中的元素上返回迭代器。
4 示例
package com.yiidian;
/**
* 一点教程网: http://www.yiidian.com
*/
/**
* java.util.ArrayDeque.descendingIterator()方法的例子
*/
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;
public class Demo {
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
热门文章
优秀文章