//TestArrayDequeAsDeque.java
//Here are the essential methods you want for a deque,
//to provide the required "double-ended queue" behavior:
//addFirst()
//addLast()
//removeFirst()
//removeLast()
//peekFirst()
//peekLast()
//size()
//isEmpty()
//clear()
//And, of course, you would expect an ArrayDeque to provide them, and
//of course it does, along with some additional useful methods such as
//contains()
//removeFirstOccurrence()
//removeLastOccurrence()
//and many other methods that are not essential for a deque, but are
//inherited from AbstractCollection<E>, Collection<E>, and Iterable<E>,
//but that should not be used if one is adhering strictly to the notion
//of what it means to be a deque.

import static java.lang.System.out;
import java.util.Deque;
import java.util.ArrayDeque;
import java.util.Arrays;

public class TestArrayDequeAsDeque
{
    public static void main(String[] args)
    {
        System.out.println("=====1=========================");
        //Create an ArrayDeque as a deque, confirm that it's empty,
        //and display its size.
        Deque<Integer> dInt = new ArrayDeque<>();
        if (dInt.isEmpty())
        {
            out.println("The deque is empty.");
        }
        else
        {
            out.println("The size of the deque is " + dInt.size() + ".");
        }

        System.out.println("=====2=========================");
        //Add some values to the front and then the back of the deque
        //and then examine the first and last values.
        Integer[] a = { 2, 7, 5 };
        Integer[] b = { 1, 4, 9 };
        for (int i : a)
        {
            dInt.addFirst(i);
        }
        for (int i : b)
        {
            dInt.addLast(i);
        }
        out.println
        (
            "The value at the front of the deque is "
            + dInt.peekFirst() + "."
        );
        out.println
        (
            "The value at the back of the deque is "
            + dInt.peekLast() + "."
        );
        if (dInt.isEmpty())
        {
            out.println("The deque is empty.");
        }
        else
        {
            out.println("The size of the deque is " + dInt.size() + ".");
        }

        System.out.println("=====3=========================");
        //Display the deque as a single entity, along with its size.
        out.println(dInt);
        if (dInt.isEmpty())
        {
            out.println("The deque is empty.");
        }
        else
        {
            out.println("The size of the deque is " + dInt.size() + ".");
        }

        System.out.println("=====4=========================");
        //Clear the deque by removing and displaying one value at a time,
        //first a value from the front, then one from the back. Note that
        //this loop will cause a runtime problem if the deque contains an
        //odd number of values!
        while (!dInt.isEmpty())
        {
            System.out.print(dInt.removeFirst() + " ");
            System.out.print(dInt.removeLast() + " ");
        }
        out.println();
        if (dInt.isEmpty())
        {
            out.println("The deque is empty.");
        }
        else
        {
            out.println("The size of the deque is " + dInt.size() + ".");
        }

        System.out.println("=====5=========================");
        //Re-create the deque as before, but this time by adding all
        //the elements of two different arrays after converting each
        //of the arrays to a list. Then display the deque and its size.
        dInt.addAll(Arrays.asList(a));
        dInt.addAll(Arrays.asList(b));
        out.println(dInt);
        out.println(dInt.size());

        System.out.println("=====6=========================");
        //Clear the deque once more and display it a final time.
        dInt.clear();
        out.println(dInt);
    }
}
/*  Output:
    =====1=========================
    The deque is empty.
    =====2=========================
    The value at the front of the deque is 5.
    The value at the back of the deque is 9.
    The size of the deque is 6.
    =====3=========================
    [5, 7, 2, 1, 4, 9]
    The size of the deque is 6.
    =====4=========================
    5 9 7 4 2 1
    The deque is empty.
    =====5=========================
    [2, 7, 5, 1, 4, 9]
    6
    =====6=========================
    []
*/

