stackofintegers.java

来自「java程序设计导论(daniel liang著) 所有偶数课后习题答案」· Java 代码 · 共 46 行

JAVA
46
字号
public class StackOfIntegers {  private int[] elements;  private int size;  /** Construct a stack with the default capacity 16 */  public StackOfIntegers() {    this(16);  }  /** Construct a stack with the specified maximum capacity */  public StackOfIntegers(int capacity) {    elements = new int[capacity];  }  /** Push a new integer into the top of the stack */  public int push(int value) {    if (size >= elements.length) {      int[] temp = new int[elements.length * 2];      System.arraycopy(elements, 0, temp, 0, elements.length);      elements = temp;    }    return elements[size++] = value;  }  /** Return and remove the top element from the stack */  public int pop() {    return elements[--size];  }  /** Return the top element from the stack */  public int peek() {    return elements[size - 1];  }  /** Test whether the stack is empty */  public boolean empty() {    return size == 0;  }  /** Return the number of elements in the stack */  public int getSize() {    return size;  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?