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.查找元素,返回元素在栈中第一次出现的位置,位置从1算起(非0)
|
package yuchen.com;
import java.util.Stack;
public class StackTest {
public static void main(String[] args){
//新建一个栈对象
Stack<Integer> s=new Stack<Integer>();
//向栈中压入 1 2 3 4 5 6
s.push(1);
s.push(2);
s.push(3);
s.push(4);
s.push(5);
s.push(6);
//在栈中搜素元素5,因为元素5是在最后倒数第二次压入的,所以其位置为2
//****注意,这里的位置是从1算起的******
System.out.println("元素5的位置:"+s.search(5));
//从栈中依次取出元素
while(!(s.isEmpty())){
int intStack = s.pop();
System.out.print(intStack+" ");
}
}
}
输出结果:版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/wuyzhen_csdn/article/details/47612097