用java实现简单bfs时发现没有用过Queue查了一下资料。
Java 1.5在java.util包中新添加了Queue接口,java1.5中使用java.util.LinkedList实现了该接口。
它的add或offer方法向队列中添加一个元素,使用remove或poll方法从队列中取出一个元素。这些方法的区别在于add和remove在队列已经满或者删除空队列元素是会抛出异常。而offer或poll则不会。
下面给出简单例子:
public void bfs(int startPoint) {
try {
EColor[startPoint] = "gray";
} catch (ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
Queue pQueue = new LinkedList();
pQueue.add(startPoint);
Integer i = startPoint;
while(!pQueue.isEmpty()) {
i = (Integer)pQueue.poll();
System.out.println(i);
for(int j = 0; j < this.size; j++) {
if((EColor[j]).equals("white") &&am