java的poll方法_Java中的队列poll()方法示例
使用poll()方法獲取并刪除Queue中的第一個(gè)元素。
創(chuàng)建一個(gè)隊(duì)列-Queue?q?=?new?LinkedList();
添加一些元素-q.add("abc");
q.add("def");
q.add("ghi");
q.add("jkl");
q.add("mno");
q.add("pqr");
q.add("stu");
q.add("vwx");
現(xiàn)在,刪除第一個(gè)元素-q.poll()
以下是實(shí)現(xiàn)poll()方法的示例-
示例import?java.util.LinkedList;
import?java.util.Queue;
public?class?Demo?{
public?static?void?main(String[]?args)?{
Queue?q?=?new?LinkedList();
q.add("abc");
q.add("def");
q.add("ghi");
q.add("jkl");
q.add("mno");
q.add("pqr");
q.add("stu");
q.add("vwx");
System.out.println("Queue?head?=?"?+?q.element());
System.out.println("Removing?element?from?queue?=?"?+?q.poll());
System.out.println("Queue?head?now?=?"?+?q.element());
System.out.println("Removing?element?from?queue?=?"?+?q.poll());
System.out.println("Queue?head?now?=?"?+?q.element());
System.out.println("\nRemaining?Queue?elements...");
Object?ob;
while?((ob?=?q.poll())?!=?null)?{
System.out.println(ob);
}
}
}
輸出結(jié)果Queue?head?=?abc
Removing?element?from?queue?=?abc
Queue?head?now?=?def
Removing?element?from?queue?=?def
Queue?head?now?=?ghi
Remaining?Queue?elements...
ghi
jkl
mno
pqr
stu
vwx
總結(jié)
以上是生活随笔為你收集整理的java的poll方法_Java中的队列poll()方法示例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 最近小程序频繁搞事情,看他们都更新了哪些
- 下一篇: Java中ConcurrentHashM