定义一个接口
package suanfa;
public interface Stack<T> {
public void clear();
public boolean isEmpty();
public T peek();
public T pop();
public void push(T elment);
public int size();
}
package suanfa;
import java.util.Arrays;
public class ArrayStack<T> implements Stack<T> {
private final int DEFAULT_SIZE=2;//数组默认的容量
private int capacity;//栈的容量
private int size;//栈的大小
private int top;//指向下一个要添加的元素的位置
private Object[] array;
public ArrayStack(){
this.capacity=DEFAULT_SIZE;
this.array=new Object[this.capacity];
this.size=0;
}
public ArrayStack(int capacity){
this.capacity=capacity;
this.array=new Object[this.capacity];
this.size=0;
}
@Override
public void clear() {
Arrays.fill(this.array, null);
this.size=0;
this.top=0;
this.capacity=DEFAULT_SIZE;
this.array=new Object[capacity];
}
@Override
public boolean isEmpty() {
return size==0;
}
@Override
public T peek() {
if(isEmpty()){
return null;
}
return (T) this.array[this.top-1];
}
@Override
public T pop() {
T elment=(T) this.array[top-1];
array[this.top-1]=null;
this.top=this.top-1;
this.size--;
return elment;
}
@Override
public void push(T elment) {
if(this.size<this.capacity){
this.array[top]=elment;
this.size++;
this.top++;
}
else { //若栈的容量不够则扩充栈的容量
addStackCapacity();
push(elment);
}
}
public void addStackCapacity(){ //栈的容量不够则扩充栈的容量
this.capacity=this.capacity+DEFAULT_SIZE;//默认增加的幅度为2
Object[] newArray=new Object[this.capacity];
System.arraycopy(this.array, 0, newArray, 0,this.array.length);//把旧数组的值复制到新的数组中
Arrays.fill(array, null);//把原来的数组的值都变为空
this.array=newArray;
}
@Override
public int size() {
return this.size;
}
/**测试栈
* @param args
*/
public static void main(String[] args) {
ArrayStack<Integer> testStack=new ArrayStack<Integer>();
testStack.push(3);
testStack.push(5);
testStack.push(2);
testStack.push(1);
testStack.push(6);
System.out.println("栈大小:"+testStack.size());
System.out.println("栈的容量:"+testStack.capacity);
System.out.println("栈顶元素:"+testStack.peek());
while(!testStack.isEmpty()){
System.out.println(testStack.pop());
}
System.out.println("栈的大小:"+testStack.size());
System.out.println("栈顶元素:"+testStack.peek());
System.out.println("栈的容量:"+testStack.capacity);
System.out.println("*****************"+"\n");
testStack.push(1);
testStack.push(6);
testStack.push(2);
System.out.println("栈的大小:"+testStack.size());
testStack.clear();
System.out.println("清空栈后栈的大小:"+testStack.size());
System.out.println("清空栈后栈的容量:"+testStack.capacity);
}
}