9. 13. 6. Stack基本用法 To find out if an element is on the stack: the search() method

来源:互联网 发布:淘宝助手批量上传宝贝 编辑:程序博客网 时间:2024/06/05 20:22
 
import java.util.Stack;public class StackLastinFirstout {public static void main(String[] args) {Stack s = new Stack();    s.push("A");    s.push("B");    s.push("C");    System.out.println(s);    System.out.println(s.pop());//移除堆栈顶部的对象,并作为此函数的值返回该对象-最后一项 C    System.out.println("Next: " + s.peek());//查看堆栈顶部的对象,但不从堆栈中移除它。最后一项 C        s.push("E");        int count = s.search("E");//返回对象在堆栈中的位置,以 1 为基数。    while(count!=-1&&count>1){    s.pop();    count--;    }    System.out.println(s);}}

原创粉丝点击