Java 类型不匹配:无法从对象转换为类对象

Java 类型不匹配:无法从对象转换为类对象,java,object,linked-list,queue,Java,Object,Linked List,Queue,如果一个对象有几个参数,并且我需要这些参数,那么我应该如何将该对象排队 例如:-BFS-->我需要对当前位置进行排队和出列,但它会给我一条错误消息 更具体地说: /*Start = new Pos(i, j, 0);*/ public static int search(char[][] maze, Pos source) { MyQueue SP = new Queue(); // p

如果一个对象有几个参数,并且我需要这些参数,那么我应该如何将该对象排队

例如:-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 interface MyQueue {
    public void enqueue(Object item);
    public Object dequeue();
    public boolean isEmpty();
    public int size();
    public Object peek();
}
当我打印出列的元素时,类似这样的内容出现在“project”。Pos@55f96302“


最后,“一般”如何将类的对象添加到链表中?

要排队,您需要将对象添加到队列中。这一个对象将保存您需要添加的所有参数

要退出队列,只需执行Pos current=SP.dequeue()

您接收到的错误是因为您的队列包含对象。您应该使其能够容纳Pos对象

public interface MyQueue {
    public void enqueue(Object item);
    public Object dequeue();
    public boolean isEmpty();
    public int size();
    public Object peek();
}
查看java.util.AbstractQueue的源代码以获取队列示例

为了回答您的最后一个问题,请选择“价值”项目。Pos@55f96302显示的原因是对象Pos没有toString()方法,例如:

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 String toString() {
        return "print here the values of xPos, yPos, and dist";
    }
}

您认为应该打印什么以及为什么?@SotiriosDelimanolis我正在尝试跟踪所有访问的节点并打印它们,例如:while(!Parent.isEmpty()){System.out.print(Parent.dequeue()+);}很抱歉给您带来不便,但我如何实现该方法?公共字符串toString(){return“在这里打印xPos、yPos和dist的值”;}每当我键入时,它会给我一个错误。。如您所述,更改成功,除我现在面临的一个错误外,没有其他错误。谢谢。。如前所述修复了它,但现在我只是使用“Integer.toString(arg0,arg1)”检索xPos和YPO,如果我还想返回dist怎么办?返回“xPos=“+xPos+”,yPos=“+yPos+”,dist=“+dist;
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 String toString() {
        return "print here the values of xPos, yPos, and dist";
    }
}