Stack
来源:互联网 发布:亚马逊中国 知乎 编辑:程序博客网 时间:2024/05/12 05:46
java中Stack常用方法,
1.empty()判断栈中是否为空。
2.peek()从栈顶取值,不删除该值
3.pop()从栈顶取值,删除改值
4.push(E item) 往stack中添加值
5.search(Object o) 查询stack中某一个值所在的位置指标,若是没有则返回-1
import java.util.Stack;public class StackTest { public static void main(String args[]) { Stack<String> stack = new Stack<String>(); System.out.println("now the stack is:" + isEmpty(stack)); stack.push("a"); stack.push("b"); stack.push("c"); stack.push("d"); stack.push("e"); System.out.println("now the stack is:" + isEmpty(stack)); System.out.println(stack.peek()); System.out.println(stack.pop()); System.out.println(stack.pop()); System.out.println(stack.search("c")); } public static String isEmpty(Stack<String> stack) { return stack.empty() ? "empty" : "not empty"; }}
运行结果:
now the stack is:empty
now the stack is:not empty
e
e
d
1
0 0
- stack
- stack
- stack
- stack
- Stack
- Stack
- Stack
- Stack
- stack
- Stack
- stack
- stack
- Stack
- stack
- Stack
- Stack
- stack
- stack.
- 蓝桥杯---神奇算式
- ios将Image保存到手机相册
- REST无状态的理解
- MariaDB+Keepalived双主高可用配置MySQL-HA
- 解析Delta Queue数据解析方法
- Stack
- ubuntu install OpenJDK/JRE
- MHTextField
- android.content.Context 含义及使用【转自博客园】
- android context获取目录详解
- 去除json中的\
- Spring整合MongoDB3.0开发(何志雄)
- MyISAM InnoDB 区别
- iPhone6分辨率与适配