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. Iterator iterator=deque.iterator();
  40. while(iterator.hasNext())
  41. {
  42. System.out.print(iterator.next()+" ");
  43. }
  44. }
  45.  
  46. public static void main(String[] args) {
  47. // TODO Auto-generated method stub
  48. Scanner scanner=new Scanner(System.in);
  49. Stack stack=new Stack(5);
  50.  
  51. while(true)
  52. {
  53. System.out.println("請輸入push、pop、peek、query任一");
  54. switch(scanner.nextLine())
  55. {
  56. case "push":
  57. if(stack.push(new String(scanner.nextLine())))
  58. {
  59. System.out.println("push一個物件");
  60. }else
  61. {
  62. System.out.println("push失敗");
  63. }
  64. break;
  65. case "pop":
  66. System.out.println(stack.pop());
  67. break;
  68. case "peek":
  69. System.out.println(stack.peek());
  70. break;
  71. case "size":
  72. System.out.println(stack.size());
  73. case "query":
  74. stack.query();
  75. break;
  76. }
  77. }
  78. }
  79. }
  80.  
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