package com.my; import java.util.Arrays; /** * 顺序栈 * @author wanjn * */ public class ArrayStack { private Object[] objects; private int size=0; public ArrayStack(){ objects = new Object[10]; } //入栈 public void push(Object value){ if (size>=10) { throw new RuntimeException("栈满了"); } objects[size++] = value; } //出栈 public Object pop(){ if (size<=0) { throw new RuntimeException("栈为空"); } Object oldValue = objects[--size]; objects[size] = null; return oldValue; } public int size(){ return size; } @Override public String toString() { return "ArrayStack [objects=" + Arrays.toString(objects) + "]"; } }