标签:stack
Stack stack = new Stack();结果:
for(int i=1; i<6; i++)
{
stack.push(String.valueOf(i));//入栈
}
int pos = stack.search("2");//查找
System.out.println("the postion of 2 is:"+pos);
stack.pop();//出栈
String val = (String)stack.peek();//栈顶数据
System.out.println("peek:"+val);
System.out.println(stack.pop());
System.out.println("判断栈是否为空,出栈");
while(!stack.isEmpty())
{
System.out.println(stack.pop());
}
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;
}
//序列版本号
private static final long serialVersionUID = 1224463164541339165L;
}
版权声明:知识在于分享,技术在于交流,转载时请留一个博主的链接就好
标签:stack
原文地址:http://blog.csdn.net/qq924862077/article/details/48039587