Java 的 Queue的offer、peek和poll
等级 4257 次按键
使用Queue接口和LinkedList通过offer()、peek()和poll()进行FIFO操作。
Java 代码
// Queue是FIFO(先进先出)数据结构// 把变量声明为Queue类型只暴露队列操作;LinkedList实现了该接口1java.util.Queue<String> queue = new java.util.LinkedList<>();// offer()将元素添加到队列末尾2queue.offer("first");// 每次offer()添加到后面: [first, second, third]3queue.offer("second");4queue.offer("third");// peek()返回队首元素而不删除它:"first"5System.out.println(queue.peek());// poll()删除并返回队首元素:"first"6System.out.println(queue.poll());// poll()之后,队列是[second, third]7System.out.println(queue);// size()返回剩余元素数量: 28System.out.println(queue.size());运行结果
first first [second, third] 2
可以用这段代码练习打字。成绩不会计入排行榜。
这段代码中的符号
这段 Java 代码中需要输入的符号及其出现次数。
.× 18(× 11)× 11;× 8"× 6<× 2>× 2=× 1