day19:队列(Queue)及其offer()方法、poll()方法、peek()方法、size()方法
(2017-01-30 08:21:21)package day05;
import
java.util.LinkedList;
import
java.util.Queue;
知识点1:
队列(Queue):保存一组元素,但是对于存取有要求,必须遵循先进先出原则
public class QueueDemo
{
public static void main(String[]
args) {
知识点2:boolean offer(E e)方法-----向队列末尾追加一个元素
queue.offer("one");
queue.offer("two");
queue.offer("three");
queue.offer("four");
System.out.println(queue);//[one,
two, three, four]
知识点3:
E
poll()方法-----从队首获取元素。注意,获取后该元素就从队列中被移除了!出队操作
String str =
queue.poll();
System.out.println(str);
// one
System.out.println(queue);
// [two,
three, four]
知识点4: E
peek()方法----- 同样可以获取队首元素,但是与poll不同的是并不会将该元素从队列中删除。
str = queue.peek();
System.out.println(str);
// two
System.out.println(queue);
// [two,
three, four]
System.out.println("开始遍历!");
//输出:开始遍历!
//被注的部分和下面的语句是一样的功能,都是按队列顺序输出队列的全部元素
// for(int
i=queue.size();i>0;i--){
// str =
queue.poll();
//
System.out.println(str);
// }
while(queue.size()>0){ //size()方法,得队列中元素个数
str = queue.poll();
System.out.println(str);
}
System.out.println(queue);
//输出: [
]
}
}