MCPcopy Index your code
hub / github.com/BruceEckel/OnJava8-Examples / main

Method main

collectiontopics/Stacks.java:13–44  ·  view source on GitHub ↗
(String[] args)

Source from the content-addressed store, hash-verified

11
12public class Stacks {
13 public static void main(String[] args) {
14 Stack<String> stack = new Stack<>();
15 for(Month m : Month.values())
16 stack.push(m.toString());
17 System.out.println("stack = " + stack);
18 // Treating a stack as a Vector:
19 stack.addElement("The last line");
20 System.out.println(
21 "element 5 = " + stack.elementAt(5));
22 System.out.println("popping elements:");
23 while(!stack.empty())
24 System.out.print(stack.pop() + " ");
25
26 // Using a LinkedList as a Stack:
27 LinkedList<String> lstack = new LinkedList<>();
28 for(Month m : Month.values())
29 lstack.addFirst(m.toString());
30 System.out.println("lstack = " + lstack);
31 while(!lstack.isEmpty())
32 System.out.print(lstack.removeFirst() + " ");
33
34 // Using the Stack class from
35 // the Collections Chapter:
36 onjava.Stack<String> stack2 =
37 new onjava.Stack<>();
38 for(Month m : Month.values())
39 stack2.push(m.toString());
40 System.out.println("stack2 = " + stack2);
41 while(!stack2.isEmpty())
42 System.out.print(stack2.pop() + " ");
43
44 }
45}
46/* Output:
47stack = [JANUARY, FEBRUARY, MARCH, APRIL, MAY, JUNE,

Callers

nothing calls this directly

Calls 7

pushMethod · 0.95
popMethod · 0.95
isEmptyMethod · 0.95
valuesMethod · 0.80
printMethod · 0.80
toStringMethod · 0.45
emptyMethod · 0.45

Tested by

no test coverage detected