集册 Java实例教程 Java Deque创建并打印ArrayDeque的内容

Java Deque创建并打印ArrayDeque的内容

欢马劈雪     最近更新时间:2020-01-02 10:19:05

855
Java Deque创建并打印ArrayDeque的内容

/**

 *ArrayDequeSample.java creates and prints the contents of an ArrayDeque.

 */// 来自 n o w    j a v a  . c o m

import java.util.ArrayDeque;

import java.util.Iterator;


public class ArrayDequeSample {

    public static void main(String[] args) {

        ArrayDeque<String> aDeque = new ArrayDeque<>();


        aDeque.addFirst("tea");

        aDeque.addFirst("milk");

        aDeque.addFirst("coffee");

        aDeque.addLast("sugar");


        /* Iterate through elements in an ArrayDeque instance */

        for (Iterator itr = aDeque.iterator(); itr.hasNext();) {

            System.out.println(itr.next());

        }

        System.out.println();


        aDeque.addFirst("juice");
        /*
        n o w j a v a . c o m - 时代Java
        */

        aDeque.addLast("honey");


        System.out.println("First Element : " + aDeque.getFirst());

        System.out.println("Last Element : " + aDeque.getLast());


        /* Removal of the Deque Elements */

        System.out

                .println("First Element(Removed):" + aDeque.removeFirst());

        System.out.println("Last Element Removed:" + aDeque.removeLast());


        System.out.println("%nPopped Element : " + aDeque.pop());

        System.out.println("%n Size of Array Deque: " + aDeque.size());


    }

}


展开阅读全文