Java Reference
In-Depth Information
67
// push elements to Stack
68
for ( int value : values)
69
{
70
System.out.printf( "%d " , value);
71
stack.push(value); // push onto integerStack
72
}
73
}
74
75
// test pop method with integer stack
76
private static void testPopInteger(Stack<Integer> stack)
77
{
78
// pop elements from stack
79
try
80
{
81
System.out.printf( "%nPopping elements from integerStack%n" );
82
int popValue; // store element removed from stack
83
84
// remove all elements from Stack
85
while ( true )
86
{
87
popValue = stack.pop(); // pop from intStack
88
System.out.printf( "%d " , popValue);
89
}
90
}
91
catch (EmptyStackException emptyStackException)
92
{
93
System.err.println();
94
emptyStackException.printStackTrace();
95
}
96
}
97
} // end class StackTest
Pushing elements onto doubleStack
1.1 2.2 3.3 4.4 5.5
Popping elements from doubleStack
5.5 4.4 3.3 2.2 1.1
EmptyStackException: Stack is empty, cannot pop
at Stack.pop(Stack.java:32)
at StackTest.testPopDouble(StackTest.java:50)
at StackTest.main(StackTest.java:17)
Pushing elements onto integerStack
1 2 3 4 5 6 7 8 9 10
Popping elements from integerStack
10 9 8 7 6 5 4 3 2 1
EmptyStackException: Stack is empty, cannot pop
at Stack.pop(Stack.java:32)
at StackTest.testPopInteger(StackTest.java:87)
at StackTest.main(StackTest.java:21)
Fig. 20.9 | Stack generic class test program. (Part 3 of 3.)
Methods testPushDouble and testPopDouble
Method testPushDouble (lines 25-36) invokes method push (line 34) to place the double
values 1.1, 2.2, 3.3, 4.4 and 5.5 from array doubleElements onto doubleStack . Autobox-
Search WWH ::




Custom Search