Java中(Queue、Deque、Stack)的特点及遍历方式


一、Queue
Queue是单端队列,遵循(FIFO)先进先出原则,最早进去的最先出来。

有限队列:有界限,大小长度受限制,常见实现类ArrayBlockingQueue;

无限队列:无界限大小限制,常见实现类LinkedList;

遍历方式(3种)
1.增强for循环
for (Object o : queue) {
System.out.println(o);
}
2.Iterator迭代器
Iterator it = queue.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
3.while循环条件判断
while (!queue.isEmpty()) {
System.out.println(queue.poll());
}
二、Deque
Deque是双端队列(Double Ended Queue),允许两头都进,两头都出。

1.增强for循环
for (Object o : deque) {
System.out.println(o);
}
2.while循环条件判断
while(deque.pollLast()!=null) {
System.out.println(deque.pollLast());
}
while(!deque.isEmpty()) {
System.out.println(deque.pollFirst());
}
 3.Iterator迭代器
Iterator it = deque.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}
总结 :Queue以及Deque都是继承于Collection,Deque是Queue的子接口。

 三、Stack
栈(Stack)是一种后进先出(LIFO:Last In First Out)的数据结构。

栈的后进先出,只能不断的往栈中压入(push)元素,最后进去的必须最早弹出(pop)。

 1.增强for循环
for(Object o : stack) {
System.out.println(o);
}
2.while循环条件判断
while(!stack.isEmpty()) {
System.out.println(stack.pop());
}
3.Iterator迭代器
Iterator it = stack.iterator();
while (it.hasNext()) {
System.out.println(it.next());
}

————————————————
版权声明:本文为CSDN博主「Anoxia_Boi」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zzz11120/article/details/124732225