您正在寻找实现Queue接口的任何类,排除
PriorityQueue和
PriorityBlockingQueue不使用FIFO算法。
可能使用(最前面添加一个)和(从前面删除一个然后返回)的linkedList是最容易使用的一个。
add``removeFirst
例如,这是一个使用linkedList排队并检索PI数字的程序:
import java.util.linkedList;class Test { public static void main(String args[]) { char arr[] = {3,1,4,1,5,9,2,6,5,3,5,8,9}; linkedList<Integer> fifo = new linkedList<Integer>(); for (int i = 0; i < arr.length; i++) fifo.add (new Integer (arr[i])); System.out.print (fifo.removeFirst() + "."); while (! fifo.isEmpty()) System.out.print (fifo.removeFirst()); System.out.println(); }}另外,如果您 知道 只想将其视为队列(没有链表的其他功能),则可以只使用
Queue接口本身:
import java.util.linkedList;import java.util.Queue;class Test { public static void main(String args[]) { char arr[] = {3,1,4,1,5,9,2,6,5,3,5,8,9}; Queue<Integer> fifo = new linkedList<Integer>(); for (int i = 0; i < arr.length; i++) fifo.add (new Integer (arr[i])); System.out.print (fifo.remove() + "."); while (! fifo.isEmpty()) System.out.print (fifo.remove()); System.out.println(); }}这样的优点是允许您用提供
Queue接口的任何类替换基础的具体类,而不必过多地更改代码。
基本更改是将的类型更改为
fifo,
Queue并使用
remove()代替
removeFirst(),后者对于
Queue接口不可用。
调用
isEmpty()仍然可以,因为它属于
Collection其
Queue派生接口。



