【发布时间】:2014-05-25 03:04:52
【问题描述】:
鉴于该对象有多个参数并且我需要这些参数,我应该如何将一个对象加入队列。
例如:- BFS--> 我需要对当前位置进行入队和出队,但它给了我一个错误消息。
更具体:
/*Start = new Pos(i, j, 0);*/
public static int search(char[][] maze, Pos source) {
MyQueue SP = new Queue();
// pushes source to Q
SP.enqueue(source);
// marks source as visited
visited[source.xPos][source.yPos] = true;
// enters main loop - checks if Q is nonempty
while (!SP.isEmpty()) {
// gets next element off of queue
Pos current = SP.dequeue(); //<< Here is the error "Type mismatch: cannot convert from Object to Pos"
// gets neighbors of current
ArrayList<Pos> neighbors = getNeighbors(maze, current);
for (Pos neighbor : neighbors) {
// checks for exit
if (maze[neighbor.xPos][neighbor.yPos] == 'E') {
EX = neighbor.xPos;
EY = neighbor.yPos;
return neighbor.dist;
}
Q.enqueue(neighbor);
}
}
// exit is not reachable
return 0;
}
位置如下:
public class Pos {
public int xPos, yPos, dist;
public Pos(int xPos, int yPos, int dist) {
this.xPos = xPos;
this.yPos = yPos;
this.dist = dist;
}
}
有关我的队列实现的更多说明:
public interface MyQueue {
public void enqueue(Object item);
public Object dequeue();
public boolean isEmpty();
public int size();
public Object peek();
}
当我打印出列元素时,会出现类似这样的内容“project.Pos@55f96302”。
最后,“一般”如何将一个类的对象添加到链表中?
【问题讨论】:
-
您认为应该打印什么以及为什么?
-
@SotiriosDelimanolis 我正在尝试跟踪所有访问过的节点并打印它们,例如:while(!Parent.isEmpty()){ System.out.print(Parent.dequeue() + " ") ; }
标签: java object linked-list queue