浅入浅出Java数据结构--栈(上)
在各种应试中经常问到的一个问题,请将字符串反转
八戒:这还不简单,利用StringBuffer/StringBuilder的reverse()函数,1秒钟搞定
public class Stack<E> extends Vector<E> { public Stack() { } public E push(E item) {addElement(item);return item; } public synchronized E pop() {E obj;int len = size();obj = peek();removeElementAt(len - 1);return obj; } public synchronized E peek() {int len = size();if (len == 0) throw new EmptyStackException();return elementAt(len - 1); } public boolean empty() {return size() == 0; } public synchronized int search(Object o) {int i = lastIndexOf(o);if (i >= 0) { return size() - i;}return -1; }}