Source of TestArrayDequeAsStack.java


  1: //TestArrayDequeAsStack.java
  2: //Compare this program with TestStack.java.
  3: //Here are the essential methods you want for a stack,
  4: //to provide the required LIFO behavior:
  5: //push()
  6: //pop()
  7: //peek()
  8: //size()
  9: //isEmpty()
 10: //clear()
 11: //So, the question is: Does an ArrayDeque provide them?
 12: //And the answer, of course, is yes ... along with many
 13: //other methods that are not essential for a stack, but
 14: //are inherited from AbstractCollection<E>, Collection<E>,
 15: //and Iterable<E>, and that should not be used if one is
 16: //adhering strictly to the notion of a LIFO stack.

 18: import static java.lang.System.out;
 19: import java.util.ArrayDeque;
 20: import java.util.Arrays;

 22: public class TestArrayDequeAsStack
 23: {
 24:     public static void main(String[] args)
 25:     {
 26:         System.out.println("=====1=========================");
 27:         //Create an ArrayDeque object to be used as a stack, confirm that
 28:         //it's empty, and display its size. Note that, unlike Deque and
 29:         //Queue, Stack is a class and not an interface, so the programmer
 30:         //is responsible for using the ArrayDeque as a stack.
 31:         ArrayDeque<Integer> sInt = new ArrayDeque<>();
 32:         if (sInt.isEmpty())
 33:         {
 34:             out.println("The stack is empty.");
 35:         }
 36:         else
 37:         {
 38:             out.println("The size of the stack is " + sInt.size() + ".");
 39:         }

 41:         System.out.println("=====2=========================");
 42:         //Add some values to the stack, and then display the
 43:         //first value and the size.
 44:         Integer[] a = { 2, 7, 5, 1, 4, 9 };
 45:         for (int i : a)
 46:         {
 47:             sInt.push(i);
 48:         }
 49:         out.println
 50:         (
 51:             "The value at the top of the stack is "
 52:             + sInt.peek() + "."
 53:         );
 54:         if (sInt.isEmpty())
 55:         {
 56:             out.println("The stack is empty.");
 57:         }
 58:         else
 59:         {
 60:             out.println("The size of the stack is " + sInt.size() + ".");
 61:         }

 63:         System.out.println("=====3=========================");
 64:         //Display the stack as a single entity, along with its size.
 65:         out.println(sInt);
 66:         if (sInt.isEmpty())
 67:         {
 68:             out.println("The stack is empty.");
 69:         }
 70:         else
 71:         {
 72:             out.println("The size of the stack is " + sInt.size() + ".");
 73:         }

 75:         System.out.println("=====4=========================");
 76:         //Clear the stack by removing and displaying one value
 77:         //at a time. Then confirm that it's empty.
 78:         //for (int i : sInt) //<--Can use this for-loop here.
 79:         //As in queue but not in priority queue.
 80:         while (!sInt.isEmpty())
 81:         {
 82:             System.out.print(sInt.pop() + " ");
 83:         }
 84:         out.println();
 85:         if (sInt.isEmpty())
 86:         {
 87:             out.println("The stack is empty.");
 88:         }
 89:         else
 90:         {
 91:             out.println("The size of the stack is " + sInt.size() + ".");
 92:         }

 94:         System.out.println("=====5=========================");
 95:         //Re-create the stack as before, but this time by adding
 96:         //all the elements of an array after converting it to a
 97:         //list. Then display the stack and its size.
 98:         sInt.addAll(Arrays.asList(a));
 99:         out.println(sInt);
100:         out.println(sInt.size());

102:         System.out.println("=====6=========================");
103:         //Clear the stack once more and display it a final time.
104:         sInt.clear();
105:         out.println(sInt);
106:     }
107: }
108: /*  Output:
109:     =====1=========================
110:     The stack is empty.
111:     =====2=========================
112:     The value at the top of the stack is 9.
113:     The size of the stack is 6.
114:     =====3=========================
115:     [9, 4, 1, 5, 7, 2]
116:     The size of the stack is 6.
117:     =====4=========================
118:     9 4 1 5 7 2
119:     The stack is empty.
120:     =====5=========================
121:     [2, 7, 5, 1, 4, 9]
122:     6
123:     =====6=========================
124:     []
125: */