package com.heatonresearch.examples.collections; import java.util.*; public class Queue { private ArrayList list = new ArrayList(); public void push(T obj) { list.add(obj); } public T pop() throws QueueException { if (size() == 0) throw new QueueException( "Tried to pop something from the queue, " + "when it was empty"); T result = list.get(0); list.remove(0); return result; } public boolean isEmpty() { return list.isEmpty(); } public int size() { return list.size(); } public void clear() { list.clear(); } } 前面的代码声明了队列类,这样它可以接收一个泛型类型。