commons-collections-----ArrayStack
ArrayStack继承于ArrayList。栈顶是最后一个入栈的元素既是链的尾,栈底是第一个入栈元素既是链头。
peek()方法:返回栈的顶部元素但不移除它(返回最后一个元素);
public Object peek() throws EmptyStackException { int n = size(); if (n <= 0) { throw new EmptyStackException(); } else { return get(n - 1); } }
public Object peek(int n) throws EmptyStackException { int m = (size() - n) - 1; if (m < 0) { throw new EmptyStackException(); } else { return get(m); } }
public Object pop() throws EmptyStackException { int n = size(); if (n <= 0) { throw new EmptyStackException(); } else { return remove(n - 1); } }
public Object push(Object item) { add(item); return item; }
public int search(Object object) { int i = size() - 1; // Current index int n = 1; // Current distance while (i >= 0) { Object current = get(i); if ((object == null && current == null) || (object != null && object.equals(current))) { return n; } i--; n++; } return -1; }
public Object get() { int size = size(); if (size == 0) { throw new BufferUnderflowException(); } return get(size - 1); }
public Object remove() { int size = size(); if (size == 0) { throw new BufferUnderflowException(); } return remove(size - 1); }