📜  实现 ArrayDeque API 的Java程序

📅  最后修改于: 2022-05-13 01:54:20.949000             🧑  作者: Mango

实现 ArrayDeque API 的Java程序

Java中的 ArrayDeque 提供了如何在 Deque 接口的实现之外使用 resizable-array。它也被称为阵列双端队列或阵列甲板。这是一个特殊的数组,它可以增长并允许用户从队列的每一侧添加或删除一个组件。

java.lang.Object

    java.util.AbstractCollection

        java.util.ArrayDeque

层次结构中 ArrayDeque 的所有实现接口为:

  • 可序列化,
  • 可克隆,
  • 可迭代,
  • 收藏
  • 双端
  • 队列

数组双端队列没有任何容量限制;它们根据需要增长以支持使用。它们不是线程安全的;在没有外部同步的情况下,它们不支持多线程并发访问。禁止空元素。这个类在用作堆栈时可能会比 Stack 快,当用作队列时可能比 LinkedList 快。

执行:

Java
// Java program to demonstrate implementation
// of ArrayDeque
  
import java.util.*;
public class ArrayDequeExample {
    public static void main(String[] args)
    {
        // Initializing an deque
        Deque de_que = new ArrayDeque(10);
  
        // add() method to insert
        de_que.add(10);
        de_que.add(20);
        de_que.add(30);
        de_que.add(40);
        de_que.add(50);
  
        for (Integer element : de_que) {
            System.out.println("Element : " + element);
        }
  
        System.out.println("Using clear() ");
  
        // clear() method
        de_que.clear();
  
        // addFirst() method to insert at start
        de_que.addFirst(564);
        de_que.addFirst(291);
  
        // addLast() method to insert at end
        de_que.addLast(24);
        de_que.addLast(14);
  
        System.out.println(
            "Above elements have been cleared");
  
        // Iterator() :
        System.out.println(
            "Elements of deque using Iterator :");
  
        for (Iterator itr = de_que.iterator();
             itr.hasNext();) {
            System.out.println(itr.next());
        }
  
        // descendingIterator() : to reverse the deque order
        System.out.println(
            "Print elements in reverse order :");
  
        for (Iterator dItr = de_que.descendingIterator();
             dItr.hasNext();)
        {
            System.out.println(dItr.next());
        }
  
        // element() method : to get Head element
        System.out.println(
            "\nHead Element using element(): "
            + de_que.element());
  
        // getFirst() method : to get Head element
        System.out.println(
            "Getting Head Element using getFirst(): "
            + de_que.getFirst());
  
        // getLast() method : to get last element
        System.out.println(
            "Getting Last Element using getLast(): "
            + de_que.getLast());
  
        // toArray() method :
        Object[] arr = de_que.toArray();
  
        System.out.println("\nArray Size : " + arr.length);
  
        System.out.print("Array elements : ");
  
        for (int i = 0; i < arr.length; i++)
            System.out.print(" " + arr[i]);
  
        // peek() method : to get head
        System.out.println("\nHead element : "
                           + de_que.peek());
  
        // poll() method : to get head
        System.out.println("Head element poll : "
                           + de_que.poll());
  
        // push() method :
        de_que.push(265);
        de_que.push(984);
        de_que.push(2365);
  
        // remove() method : to get head
        System.out.println("Head element remove : "
                           + de_que.remove());
  
        System.out.println("Final Array: " + de_que);
    }
}


输出
Element : 10
Element : 20
Element : 30
Element : 40
Element : 50
Using clear() 
Above elements have been cleared
Elements of deque using Iterator :
291
564
24
14
Print elements in reverse order :
14
24
564
291

Head Element using element(): 291
Getting Head Element using getFirst(): 291
Getting Last Element using getLast(): 14

Array Size : 4
Array elements :  291 564 24 14
Head element : 291
Head element poll : 291
Head element remove : 2365
Final Array: [984, 265, 564, 24, 14]