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) + "]";
    }
    
    
}

 

 posted on 2018-01-26 16:44  改变一下  阅读(147)  评论(0编辑  收藏  举报