📜  具有最大大小的 Java 堆栈 - Java 代码示例

📅  最后修改于: 2022-03-11 14:52:08.959000             🧑  作者: Mango

代码示例1
import java.util.Stack;

public class SizedStack extends Stack {
    private int maxSize;

    public SizedStack(int size) {
        super();
        this.maxSize = size;
    }

    @Override
    public T push(T object) {
        //If the stack is too big, remove elements until it's the right size.
        while (this.size() >= maxSize) {
            this.remove(0);
        }
        return super.push(object);
    }
}