fork download
  1. package chapter8;
  2. import java.util.*;
  3. public class Stack {
  4.  
  5. private Deque deque=new ArrayDeque();
  6. private int capacity;
  7.  
  8. public Stack(int capacity)
  9. {
  10. this.capacity=capacity;
  11. }
  12.  
  13. public boolean push(Object o)
  14. {
  15. if(deque.size()+1>capacity)
  16. {
  17. return false;
  18. }
  19. return deque.offerLast(o);
  20. }
  21.  
  22. public Object pop()
  23. {
  24. return deque.pollLast();
  25. }
  26.  
  27. public Object peek()
  28. {
  29. return deque.peekLast();
  30. }
  31.  
  32. public int size()
  33. {
  34. return deque.size();
  35. }
  36.  
  37. public void query()
  38. {
  39. while(deque.iterator().hasNext())
  40. {
  41. System.out.print(deque.iterator().next()+" ");
  42. }
  43. }
  44.  
  45. public static void main(String[] args) {
  46. // TODO Auto-generated method stub
  47. Scanner scanner=new Scanner(System.in);
  48. Stack stack=new Stack(5);
  49.  
  50. while(true)
  51. {
  52. System.out.println("請輸入push、pop、peek、query任一");
  53. switch(scanner.nextLine())
  54. {
  55. case "push":
  56. if(stack.push(new String(scanner.nextLine())))
  57. {
  58. System.out.println("push一個物件");
  59. }else
  60. {
  61. System.out.println("push失敗");
  62. }
  63. break;
  64. case "pop":
  65. System.out.println(stack.pop());
  66. break;
  67. case "peek":
  68. System.out.println(stack.peek());
  69. break;
  70. case "size":
  71. System.out.println(stack.size());
  72. case "query":
  73. stack.query();
  74. break;
  75. }
  76. }
  77. }
  78. }
  79.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:3: error: class Stack is public, should be declared in a file named Stack.java
public class Stack {
       ^
Note: Main.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
stdout
Standard output is empty