基于ArrayList实现的ArrayStack
参考接口
public interface myStack<T> { void push(T element); T peek(); T pop(); Integer search(T element); Integer size(); void clear(); Boolean isEmpty(); }
实现代码
import java.util.ArrayList; public class MyArrayStack<T> implements myStack<T>{ private ArrayList<T> core; public MyArrayStack() { this(10); } public MyArrayStack(int capacity) { core = new ArrayList<>(capacity); } @Override public void push(T element) { core.add(element); } @Override public T peek() { return core.get(core.size() - 1); } @Override public T pop() { return core.remove(core.size() - 1); } @Override public Integer search(T element) { return core.lastIndexOf(element); } @Override public Integer size() { return core.size(); } @Override public void clear() { core.clear(); } @Override public Boolean isEmpty() { return core.isEmpty(); } public static void main(String[] args) { MyArrayStack<Integer> stack = new MyArrayStack<>(); for (int i = 0; i < 10; i ++) { stack.push(10 - i); } // System.out.println(stack.size()); // System.out.println(stack.peek()); // stack.pop(); // System.out.println(stack.peek()); // for (int i = 0 ; i < stack.size(); i ++) { // System.out.println(stack.pop()); // } } }
相关推荐
xiaouncle 2020-07-31
源码物语 2020-07-18
XGQ 2020-06-21
fengyun 2020-06-14
OldBowl 2020-06-11
zagnix 2020-06-04
xhao 2020-06-04
付春杰Blog 2020-05-31
ilovewqf 2020-05-30
shenxiuwen 2020-05-29
XGQ 2020-05-26
zhuyonge 2020-05-09
zcpHappy 2020-04-30
luohui 2020-04-29
fraternityjava 2020-04-29
willluckysmile 2020-04-24
shayuchaor 2020-04-20
fengyun 2020-04-17