![](https://i-blog.csdnimg.cn/blog_migrate/265c784b027981965979e37d1c8f90e8.jpeg)
package pack1;
import java.util.ArrayList;
public class MyStack<E> extends ArrayList<E> {
public MyStack() {
}
public MyStack(int initialCapacity) {
super(initialCapacity);
}
public boolean push(E element) {
if (element == null) return false;
super.add(element);
return true;
}
public E pop() {
if (super.size() == 0) return null;
return super.remove(super.size() - 1);
}
public E peek() {
if (super.size() == 0) return null;
return super.get(super.size() - 1);
}
public int getSize() {
return super.size();
}
public boolean isEmpty() {
return super.size() == 0;
}
@Override
public String toString() {
return super.toString();
}
}
package pack1;
public class TestMyStack {
public static void main(String[] args) {
MyStack<String> stack = new MyStack<>();
stack.push("One");
stack.push("Two");
stack.push("Three");
stack.push("Four");
stack.push("Five");
while (!stack.isEmpty())
System.out.println(stack.pop());
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/e967c49b6aa627a188bcd51e74870acf.png)