Source of TestArrayDequeAsDeque.java


  1: //TestArrayDequeAsDeque.java
  2: //Here are the essential methods you want for a deque,
  3: //to provide the required "double-ended queue" behavior:
  4: //addFirst()
  5: //addLast()
  6: //removeFirst()
  7: //removeLast()
  8: //peekFirst()
  9: //peekLast()
 10: //size()
 11: //isEmpty()
 12: //clear()
 13: //And, of course, you would expect an ArrayDeque to provide them, and
 14: //of course it does, along with some additional useful methods such as
 15: //contains()
 16: //removeFirstOccurrence()
 17: //removeLastOccurrence()
 18: //and many other methods that are not essential for a deque, but are
 19: //inherited from AbstractCollection<E>, Collection<E>, and Iterable<E>,
 20: //but that should not be used if one is adhering strictly to the notion
 21: //of what it means to be a deque.

 23: import static java.lang.System.out;
 24: import java.util.Deque;
 25: import java.util.ArrayDeque;
 26: import java.util.Arrays;

 28: public class TestArrayDequeAsDeque
 29: {
 30:     public static void main(String[] args)
 31:     {
 32:         System.out.println("=====1=========================");
 33:         //Create an ArrayDeque as a deque, confirm that it's empty,
 34:         //and display its size.
 35:         Deque<Integer> dInt = new ArrayDeque<>();
 36:         if (dInt.isEmpty())
 37:         {
 38:             out.println("The deque is empty.");
 39:         }
 40:         else
 41:         {
 42:             out.println("The size of the deque is " + dInt.size() + ".");
 43:         }

 45:         System.out.println("=====2=========================");
 46:         //Add some values to the front and then the back of the deque
 47:         //and then examine the first and last values.
 48:         Integer[] a = { 2, 7, 5 };
 49:         Integer[] b = { 1, 4, 9 };
 50:         for (int i : a)
 51:         {
 52:             dInt.addFirst(i);
 53:         }
 54:         for (int i : b)
 55:         {
 56:             dInt.addLast(i);
 57:         }
 58:         out.println
 59:         (
 60:             "The value at the front of the deque is "
 61:             + dInt.peekFirst() + "."
 62:         );
 63:         out.println
 64:         (
 65:             "The value at the back of the deque is "
 66:             + dInt.peekLast() + "."
 67:         );
 68:         if (dInt.isEmpty())
 69:         {
 70:             out.println("The deque is empty.");
 71:         }
 72:         else
 73:         {
 74:             out.println("The size of the deque is " + dInt.size() + ".");
 75:         }

 77:         System.out.println("=====3=========================");
 78:         //Display the deque as a single entity, along with its size.
 79:         out.println(dInt);
 80:         if (dInt.isEmpty())
 81:         {
 82:             out.println("The deque is empty.");
 83:         }
 84:         else
 85:         {
 86:             out.println("The size of the deque is " + dInt.size() + ".");
 87:         }

 89:         System.out.println("=====4=========================");
 90:         //Clear the deque by removing and displaying one value at a time,
 91:         //first a value from the front, then one from the back. Note that
 92:         //this loop will cause a runtime problem if the deque contains an
 93:         //odd number of values!
 94:         while (!dInt.isEmpty())
 95:         {
 96:             System.out.print(dInt.removeFirst() + " ");
 97:             System.out.print(dInt.removeLast() + " ");
 98:         }
 99:         out.println();
100:         if (dInt.isEmpty())
101:         {
102:             out.println("The deque is empty.");
103:         }
104:         else
105:         {
106:             out.println("The size of the deque is " + dInt.size() + ".");
107:         }

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

118:         System.out.println("=====6=========================");
119:         //Clear the deque once more and display it a final time.
120:         dInt.clear();
121:         out.println(dInt);
122:     }
123: }
124: /*  Output:
125:     =====1=========================
126:     The deque is empty.
127:     =====2=========================
128:     The value at the front of the deque is 5.
129:     The value at the back of the deque is 9.
130:     The size of the deque is 6.
131:     =====3=========================
132:     [5, 7, 2, 1, 4, 9]
133:     The size of the deque is 6.
134:     =====4=========================
135:     5 9 7 4 2 1
136:     The deque is empty.
137:     =====5=========================
138:     [2, 7, 5, 1, 4, 9]
139:     6
140:     =====6=========================
141:     []
142: */