day19:栈(Stack)、双端队列(Deque)、push()方法、pop()方法、peek()方法
(2017-01-30 08:40:43)package day05;
import
java.util.Deque;
import
java.util.LinkedList;
知识点1:
1、栈----存储一组元素,存取必须遵循先进后出原则
2、栈通常用来实现具有"后退"功能的地方。
public class StackDemo
{
public static void main(String[]
args) {
知识点2:
1、双端队列(Deque)是Queue的子接口。
2、双端队列的特点是,队列的两端都可以进出元素,入队(offer),出队(poll)
3、若将Deque限制为只能从一段入队和出队,就形成了栈(Stack)的数据结构
4、对于栈而言,入栈(push),出栈(pop)
知识点3:
1、void
push(E e)方法----将元素"压入"栈中,入栈操作。
2、新入栈的元素会在栈顶(栈中第一个元素)
stack.push("one");
stack.push("two");
stack.push("three");
stack.push("four");
System.out.println(stack);
// [four,
three, two, one]
知识点4:E
pop()方法----出栈操作,获取栈顶元素,获取后该元素就从栈中被删除了
String str =
stack.pop();
System.out.println(str);
//four
System.out.println(stack);
//[three,
two, one]
知识点5:同样可以使用peek引用栈顶元素,而不做出栈操作
str = stack.peek();
System.out.println(str);
// three
while(stack.size()>0){
System.out.println(stack.pop());
}
System.out.println(stack);
//[]
}
}