java队列

add 增加一个元素 如果队列已满,则抛出一个IllegalSlabEepeplian异常

remove 移除并返回队列头部的元素 如果队列为空,则抛出一个NoSuchElementException异常

element 返回队列头部的元素 如果队列为空,则抛出一个NoSuchElementException异常

offer 添加一个元素并返回true 如果队列已满,则返回false
poll 移除并返问队列头部的元素 如果队列为空,则返回null
peek 返回队列头部的元素 如果队列为空,则返回null
put 添加一个元素 如果队列满,则阻塞
take 移除并返回队列头部的元素 如果队列为空,则阻塞

举个栗子:

package hhhhh;
import java.util.LinkedList;
import java.util.Queue;

publicclass Main2 {
publicstaticvoid main(String[] args) {
// TODO Auto-generated method stub
Queue<String>q=new LinkedList<String>();
q.offer("A");
for(inti=2;i<=9;i++)
{
char []s={(char) ('0'+i)};
String str=new String(s);
q.offer(str);
}
q.offer("10");
q.offer("J");
q.offer("Q");
q.offer("K");
System.out.println("The order that others were removed:");
while(q.size()!=1)
{
for(inti=1;i<10;i++)
{
String temp=q.poll();
q.offer(temp);
}
String temp=q.poll();
System.out.println(temp);
}
System.out.println("the last one is:");
System.out.println(q.poll());
}
}

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!

尺取法 Previous
AC自动机 Next