不移除元素遍历栈和队列
来源:互联网 发布:田中真弓等级知乎 编辑:程序博客网 时间:2024/06/05 02:53
一、Queue的遍历
import java.util.Queue;
import java.util.concurrent.LinkedBlockingQueue;
/**
* 队列的遍历
*
* @author leizhimin 2009-7-22 15:05:14
*/
public class TestQueue {
public static void main(String[] args) {
Queue<Integer> q = new LinkedBlockingQueue<Integer>();
//初始化队列
for (int i = 0; i < 5; i++) {
q.offer(i);
}
System.out.println("-------1-----");
//集合方式遍历,元素不会被移除
for (Integer x : q) {
System.out.println(x);
}
System.out.println("-------2-----");
//队列方式遍历,元素逐个被移除
while (q.peek() != null) {
System.out.println(q.poll());
}
}
}
import java.util.concurrent.LinkedBlockingQueue;
/**
* 队列的遍历
*
* @author leizhimin 2009-7-22 15:05:14
*/
public class TestQueue {
public static void main(String[] args) {
Queue<Integer> q = new LinkedBlockingQueue<Integer>();
//初始化队列
for (int i = 0; i < 5; i++) {
q.offer(i);
}
System.out.println("-------1-----");
//集合方式遍历,元素不会被移除
for (Integer x : q) {
System.out.println(x);
}
System.out.println("-------2-----");
//队列方式遍历,元素逐个被移除
while (q.peek() != null) {
System.out.println(q.poll());
}
}
}
-------1-----
0
1
2
3
4
-------2-----
0
1
2
3
4
Process finished with exit code 0
0
1
2
3
4
-------2-----
0
1
2
3
4
Process finished with exit code 0
二、Stack的遍历
import java.util.Stack;
/**
* 栈的遍历
*
* @author leizhimin 2009-7-22 14:55:20
*/
public class TestStack {
public static void main(String[] args) {
Stack<Integer> s = new Stack<Integer>();
for (int i = 0; i < 10; i++) {
s.push(i);
}
//集合遍历方式
for (Integer x : s) {
System.out.println(x);
}
System.out.println("------1-----");
//栈弹出遍历方式
// while (s.peek()!=null) { //不健壮的判断方式,容易抛异常,正确写法是下面的
while (!s.empty()) {
System.out.println(s.pop());
}
System.out.println("------2-----");
//错误的遍历方式
// for (Integer x : s) {
// System.out.println(s.pop());
// }
}
}
/**
* 栈的遍历
*
* @author leizhimin 2009-7-22 14:55:20
*/
public class TestStack {
public static void main(String[] args) {
Stack<Integer> s = new Stack<Integer>();
for (int i = 0; i < 10; i++) {
s.push(i);
}
//集合遍历方式
for (Integer x : s) {
System.out.println(x);
}
System.out.println("------1-----");
//栈弹出遍历方式
// while (s.peek()!=null) { //不健壮的判断方式,容易抛异常,正确写法是下面的
while (!s.empty()) {
System.out.println(s.pop());
}
System.out.println("------2-----");
//错误的遍历方式
// for (Integer x : s) {
// System.out.println(s.pop());
// }
}
}
0
1
2
3
4
------1-----
4
3
2
1
0
------2-----
Process finished with exit code 0
1
2
3
4
------1-----
4
3
2
1
0
------2-----
Process finished with exit code 0
阅读全文
0 0
- 不移除元素遍历栈和队列
- C#遍历集合移除元素
- 遍历集合时移除一个元素
- 遍历List并移除其中的某些元素
- 如何边遍历边移除Collection中的元素
- 动态添加和移除元素
- Map---遍历移除
- iterator移除元素
- List移除元素
- 计蒜客 元素移除
- 移除DOM元素
- QVector移除元素
- 元素移除
- 移除元素
- 移除0元素
- 元素移除
- 计蒜客 -- 元素移除
- 计蒜客 元素移除
- Apache 防盗链配置
- 【nginx】-cache loader process进程分析
- 全局异常捕获器
- 代理模式
- java 环境配置
- 不移除元素遍历栈和队列
- PullToRefresh ScrollView的GridView和轮播图组合Fragment中代码
- Android全局异常捕获
- swift4 视频转gif
- git 本地项目push到GITHUB
- spring Boot测试的最佳实践和测试架构的启发(JUnit4和mockito,包括MockMvc)
- Android安卓自定义控件实现点击事件等逻辑
- 文章标题 计算几何
- 苹果电脑装win7系统问题