public class TestStack
1: //TestStack.java
2: //Compare this program with TestArrayDequeAsStack.java.
3: //Here are the minimal essential methods you need for a
4: //stack to provide the required LIFO behavior:
5: //push()
6: //pop()
7: //peek()
8: //empty() Note: Not isEmpty() like in ArrayDeque.
10: //search()
11: //Note that the search() method is available in the Java Stack interface,
12: //but a method like this should not be part of *any* Stack interface
13: //because it violates the essential LIFO behavior that characterizes
14: //the stack container type.
15: //Note: The following three methods are not in the Stack interface itself
16: //but are available from Vector, from which Stack is inherited.
17: //size()
18: //clear()
19: //addAll()
20: //remove()
21: import static java.lang.System.out;
22: import java.util.Arrays;
23: import java.util.Stack;
25: public class TestStack
26: {
27: public static void main(String[] args)
28: {
29: System.out.println("=====1=========================");
30: //Create a stack, confirm that it's empty, and display its size.
31: Stack<Integer> sInt = new Stack<>();
32: if (sInt.empty())
33: {
34: out.println("The stack is empty.");
35: }
36: out.println("The size of the stack is " + sInt.size() + ".");
38: System.out.println("=====2=========================");
39: //Add some values to the stack, and then display the
40: //top value and the size (and also do some searching,
41: //which should not be possible for a stack!).
42: Integer[] a = {2, 7, 5, 1, 4, 9};
43: for (int i : a) sInt.push(i);
45: out.println
46: (
47: "The value at the top of the stack is "
48: + sInt.peek() + "."
49: );
50: out.println("The size of the stack is " + sInt.size() + ".");
52: int locationIndex = sInt.search(3);
53: if (locationIndex == -1)
54: out.println("The value 3 is not in the stack.");
55: else
56: out.println
57: (
58: "The value 3 is at position "
59: + sInt.search(3) + " in the stack."
60: );
62: locationIndex = sInt.search(7);
63: if (locationIndex == -1)
64: out.println("The value 7 is not in the stack.");
65: else
66: out.println
67: (
68: "The value 7 is at position "
69: + sInt.search(7) + " in the stack."
70: );
72: System.out.println("=====3=========================");
73: //Display the stack as a single entity, along with its size.
74: out.println(sInt);
75: out.println("The size of the stack is " + sInt.size() + ".");
77: System.out.println("=====4=========================");
78: //Clear the stack by removing and displaying one value
79: //at a time. Then confirm that it's empty.
80: while (!sInt.empty()) //<--Cannot use for (int i : sInt) here!
81: System.out.print(sInt.pop() + " ");
82: out.println();
83: out.println
84: (
85: "The stack is " + (sInt.empty() ? "" : " not ")
86: + "empty."
87: );
88: out.println("The size of the stack is " + sInt.size() + ".");
90: System.out.println("=====5=========================");
91: //Re-create the stack as before, but this time by adding
92: //all the elements of an array after converting it to a
93: //list. Then display the stack and its size.
94: sInt.addAll(Arrays.asList(a));
95: out.println(sInt);
96: out.println(sInt.size());
98: System.out.println("=====6=========================");
99: //Clear the stack once more and display it a final time.
100: sInt.clear();
101: out.println(sInt);
103: //The following code segments illustrate some additional things
104: //you should not be able to do with a stack, since they violate
105: //the notion of "stackness". The fact that you can do them is a
106: //reflection of the fact that, in Java, the Stack class inherits
107: //from the Vector class, in which you *can* do these things.
108: System.out.println("=====7=========================");
109: out.println(sInt.capacity());
110: sInt.addAll(Arrays.asList(a));
111: out.println(sInt.size());
112: out.println(sInt);
113: System.out.println("=====8=========================");
114: sInt.sort((m, n) -> m - n);
115: out.println(sInt);
117: System.out.println("=====9=========================");
118: out.println(sInt.remove(2) + " removed from stack.");
119: out.println(sInt);
120: }
121: }
122: /* Output:
123: =====1=========================
124: The stack is empty.
125: =====2=========================
126: The value at the top of the stack is 9.
127: The size of the stack is 6.
128: The value 3 is not in the stack.
129: The value 7 is at position 5 in the stack.
130: =====3=========================
131: [2, 7, 5, 1, 4, 9]
132: The size of the stack is 6.
133: =====4=========================
134: 9 4 1 5 7 2
135: The stack is empty.
136: =====5=========================
137: [2, 7, 5, 1, 4, 9]
138: 6
139: =====6=========================
140: []
141: =====7=========================
142: 10
143: 6
144: [2, 7, 5, 1, 4, 9]
145: =====8=========================
146: [1, 2, 4, 5, 7, 9]
147: =====9=========================
148: 4 removed from stack.
149: [1, 2, 5, 7, 9]
150: */