Stack Implementation in JAVA

This is my Java implementation of the Stack data structure using the Java dynamic array data structure, ArrayList. It is named Deck in order to differentiate it from the Java standard library Stack. It supports the common methods such as isEmpty, push, pop, peek, and generics.

public class Deck<T> {
    List<T> buffer;

    public Deck() {
        this.buffer = new ArrayList<T>();
    }

    public boolean isEmpty() {
        return buffer.size() == 0;
    }

    public void push(T k) {
        buffer.add(k);
    }

    public T pop() {
        if(isEmpty())
            throw new EmptyStackException();

        T k = buffer.get(buffer.size() - 1);
        buffer.remove(buffer.size() - 1);
        return k;
    }

    public T peek() {
        if(isEmpty())
            throw new EmptyStackException();

        return buffer.get(buffer.size() - 1);
    }
}

This article is my 7th oldest. It is 112 words long, and it’s got 0 comments for now.