📄 arraystack.java
字号:
package stackqueue;
public class ArrayStack{
public ArrayStack(){
theArray = (int[]) new int[DEFAULT_CAPACITY];
topOfStack = -1;
}
public boolean isEmpty(){
return topOfStack == -1;
}
public void makeEmpty(){
topOfStack = -1;
}
public int top() throws UnderflowException{
if(isEmpty())
throw new UnderflowException("ArrayStack top.");
return theArray[topOfStack];
}
public void pop() throws UnderflowException{
if(isEmpty())
throw new UnderflowException("ArrayStack pop.");
topOfStack--;
}
public int topAndPop() throws UnderflowException{
if(isEmpty())
throw new UnderflowException("ArrayStack topAndPop.");
return theArray[topOfStack--];
}
public void push(int x){
if(topOfStack + 1 == theArray.length)
doubleArray();
theArray[++topOfStack] = x;
}
private void doubleArray(){
int[] oldArray = theArray;
this.theArray = new int[this.theArray.length * 2];
for(int i = 0; i < oldArray.length; i++){
this.theArray[i] = oldArray[i];
}
}
private int[] theArray;
private int topOfStack;
private static final int DEFAULT_CAPACITY = 10;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -