public class Stack<E> extends Vector<E>
Stack是Vector的子类,是一个标准的先进后出的栈。
Stack有几个自己特有的方法。
Modifier and Type | Method and Description |
---|---|
boolean | empty()
Tests if this stack is empty.
|
E | peek()
Looks at the object at the top of this stack without removing it from the stack.
|
E | pop()
Removes the object at the top of this stack and returns that object as the value of this function.
|
E | push(E item)
Pushes an item onto the top of this stack.
|
int | search(Object o)
Returns the 1-based position where an object is on this stack.
|
import java.util.Stack;
public class Test {
public static void main(String[] args){
Stack stack=new Stack();
System.out.println("stack为空吗:"+stack.empty());
stack.push(1);
System.out.println("放进一个元素后stack为空吗:"+stack.empty());
stack.push('1');
stack.push("1");
stack.push("aa");
System.out.println("栈顶元素:"+stack.peek());
System.out.println("11的位置"+stack.search("11"));
System.out.println("aa的位置"+stack.search("aa"));
System.out.println("string型1的位置"+stack.search("1"));
System.out.println("字符型1的位置"+stack.search('1'));
System.out.println("整形1的位置"+stack.search(1));
System.out.println("拿出了"+stack.pop());
System.out.println("拿出了"+stack.pop());
System.out.println("拿出了"+stack.pop());
System.out.println("拿出了"+stack.pop());
System.out.println("stack为空吗:"+stack.empty());
}
}
stack为空吗:true
放进一个元素后stack为空吗:false
栈顶元素:aa
11的位置-1
aa的位置1
string型1的位置2
字符型1的位置3
整形1的位置4
拿出了aa
拿出了1
拿出了1
拿出了1
stack为空吗:true