Mercurial Hosting > nabble
diff src/fschmidt/util/java/ArrayStack.java @ 68:00520880ad02
add fschmidt source
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Sun, 05 Oct 2025 17:24:15 -0600 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/fschmidt/util/java/ArrayStack.java Sun Oct 05 17:24:15 2025 -0600 @@ -0,0 +1,35 @@ +package fschmidt.util.java; + +import java.util.Collection; +import java.util.List; +import java.util.ArrayList; +import java.util.EmptyStackException; + + +public class ArrayStack<E> extends ArrayList<E> implements Stack<E> { + + public ArrayStack() {} + + public ArrayStack(Collection<E> c) { + super(c); + } + + @Override public void push(E item) { + add(item); + } + + @Override public E pop() throws EmptyStackException { + int len = size(); + if (len == 0) + throw new EmptyStackException(); + return remove(len - 1); + } + + @Override public E peek() throws EmptyStackException { + int len = size(); + if (len == 0) + throw new EmptyStackException(); + return get(len - 1); + } + +}