java集合类——Stack类
2016-03-04 17:10
375 查看
查看java的API文档,Stack继承Vector类。
栈的特点是后进先出。
API中Stack自身的方法不多,基本跟栈的特点有关。
Java代码
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("1");
stack.push("2");
stack.push("3");
stack.push("4");
stack.push("5");
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("2"));
}
public static String isEmpty(Stack<String> stack) {
return stack.empty() ? "empty" : "not empty";
}
}
输出为:
Java代码
now the stack is empty
now the stack is not empty
5
5
4
2
可以看出
栈的特点是后进先出。
API中Stack自身的方法不多,基本跟栈的特点有关。
Java代码
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("1");
stack.push("2");
stack.push("3");
stack.push("4");
stack.push("5");
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("2"));
}
public static String isEmpty(Stack<String> stack) {
return stack.empty() ? "empty" : "not empty";
}
}
输出为:
Java代码
now the stack is empty
now the stack is not empty
5
5
4
2
可以看出
相关文章推荐
- SpringMVC使用@ResponseBody时返回json的日期格式
- java内存溢出和内存泄露
- Spring Cache
- 关于从eclipse部署已经发布到Android studio的坑
- 注释驱动的 Spring cache 缓存介绍
- java快速排序,优化
- Java多线程之wait()、notify()、notifyAll()
- Java 多线程(二)——创建线程(Thread、Runnable、Callable)
- Eclipse启动异常:Java was started but returned exit code=13
- java string 首字母大写方法
- 如何学好 java
- springMVC中调用ModelAndView.setViewName()方法不起作用,发现路径不能被改变!!
- java中新建对象的五种方式
- Create SpringMVC+Mybatic
- struts2学习笔记-----action名称的搜索顺序
- java,类的构造方法
- 自定义复杂bean注册到spring
- 浅谈Spring事务隔离级别
- Java笔记---易混淆的概念(一:栈和堆)
- JAVA IO最常用类及实例