【问题标题】:Type mismatch: cannot convert from Object to Class object类型不匹配:无法从 Object 转换为 Class 对象
【发布时间】: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


【解决方案1】:

要入队,您需要将一个对象添加到队列中。这一个对象将包含您需要添加的所有参数。

要出队,只需执行 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 的源代码,了解队列的示例。

为了解决您的最后一个问题,显示值“project.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";
    }
}

【讨论】:

  • 很抱歉给您带来不便,但我该如何实现该方法? public String toString() { return "这里打印 xPos、yPos 和 dist 的值"; } 每当我输入它时,它就会给我一个错误。如您所说,更改成功,除了我现在面临的一个错误之外,还没有错误。
  • 谢谢 .. 已按说明修复,但现在我只使用“Integer.toString(arg0, arg1)”检索 xPos 和 yPos 如果我还想返回 dist 怎么办?
  • return "xPos="+xPos+", yPos="+yPos+", dist="+dist;
猜你喜欢
  • 2014-04-07
  • 1970-01-01
  • 2016-04-08
  • 2018-11-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-29
相关资源
最近更新 更多