「java入門」java.util.ArrayDeque.descendingIterator()のサンプルコード

Javaコード

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Iterator;

public class DescendingIteratorDemo {

public static void main(String[] args) {
// create an empty array deque with an initial capacity
Deque<Integer> cft= new ArrayDeque<Integer>(8);

// use add() method to add elements in the deque
cft.add(36);
cft.add(42);
cft.add(55);
cft.add(60);

//iterator() is used to print all the elements
//next() returns the next element on each iteration
System.out.println(“iteratorで要素を出力:");

for(Iterator<Integer> itr = cft.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(“要素を逆順:");

for(Iterator<Integer> descItr =cft.descendingIterator();descItr.hasNext();) {
System.out.println(descItr.next());
}
}

}

結果

iteratorで要素を出力:
36
42
55
60
要素を逆順:
60
55
42
36

Java

Posted by arkgame