java stack with max size

Solutions on MaxInterview for java stack with max size by the best coders in the world

showing results for - "java stack with max size"
Beatrice
13 Mar 2017
1import java.util.Stack;
2
3public class SizedStack<T> extends Stack<T> {
4    private int maxSize;
5
6    public SizedStack(int size) {
7        super();
8        this.maxSize = size;
9    }
10
11    @Override
12    public T push(T object) {
13        //If the stack is too big, remove elements until it's the right size.
14        while (this.size() >= maxSize) {
15            this.remove(0);
16        }
17        return super.push(object);
18    }
19}
20