Type mismatch: cannot convert from Object to Class object

user3672907

How should I enqueue an object given that this object has several parameters and I need these parameters.

Eg:- BFS--> I need to enqueue and dequeue the current position but it gives me an error msg.

To be more specific :

                               /*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;
        }

Where Pos as follwoing :

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;
}
}

For more illustration of my queue implementation :

public interface MyQueue {
    public void enqueue(Object item);
    public Object dequeue();
    public boolean isEmpty();
    public int size();
    public Object peek();
}

When I print dequeued elements , something like this appears "project.Pos@55f96302".

Finally, how "in general" could add an object of a class to linked list?

Alexandre Santos

To enqueue, you add an object to the queue. This one object will hold all parameters you need to add.

To dequeue, simply do Pos current = SP.dequeue();

The error you are receiving it is because your queue holds Objects. You should make it to hold Pos objects.

public interface MyQueue {
    public void enqueue(Object item);
    public Object dequeue();
    public boolean isEmpty();
    public int size();
    public Object peek();
}

Take a look at the source code of java.util.AbstractQueue for an example of Queues.

To address your last question, the value "project.Pos@55f96302" is displayed because your object Pos doesn't have a toString() method, such as:

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";
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Type mismatch: cannot convert from Object to Class object

From Dev

Type mismatch: cannot convert from element type Object to Cookie

From Dev

type mismatch cannot convert from element type object to string

From Dev

Java - Type mismatch: cannot convert from element type Object to String

From Dev

type mismatch cannot convert from element type object to string

From Dev

Type mismatch: cannot convert from element type Object to Cookie

From Dev

Java - Type mismatch: cannot convert from element type Object to String

From Dev

Type mismatch: cannot convert from element type Object to List

From Dev

Java generics. Type mismatch: cannot convert from object to

From Dev

Type mismatch: cannot convert from Optional<Object> to BasketDTO

From Dev

spark Type mismatch: cannot convert from JavaRDD<Object> to JavaRDD<String>

From Dev

error : Type mismatch: cannot convert from Object to JSONObject

From Dev

Java generics. Type mismatch: cannot convert from object to

From Dev

Type mismatch: cannot convert from Set<Object> to Set<Long>

From Dev

Type mismatch: cannot convert from Class<capture#1-of ?> to Class<?>[]

From Dev

Type mismatch: cannot convert from Class<Parameterized> to Class<? extends Runner>

From Dev

issue with java 8 collectors Type mismatch: cannot convert from List<Object> to List<String>

From Dev

Type mismatch on java and cannot instantiate the type of object

From Dev

How can I fix (The left-hand side of an assignment must be a variable) and (Type mismatch: cannot convert from Object to String) in this code?

From Dev

Type mismatch: cannot convert from boolean to int

From Dev

Type mismatch: cannot convert from ListFragment to Fragment

From Dev

Type mismatch: cannot convert from long to int

From Dev

type mismatch: cannot convert from double to Double

From Dev

Type mismatch cannot convert from String to String[]

From Dev

Type mismatch: cannot convert from Scanner to boolean

From Dev

Type mismatch: cannot convert from void to Integer

From Dev

Type mismatch: cannot convert from int to TextView

From Dev

Type mismatch: cannot convert from void to int

From Dev

Type mismatch : cannot convert from double[][] to double[]

Related Related

  1. 1

    Type mismatch: cannot convert from Object to Class object

  2. 2

    Type mismatch: cannot convert from element type Object to Cookie

  3. 3

    type mismatch cannot convert from element type object to string

  4. 4

    Java - Type mismatch: cannot convert from element type Object to String

  5. 5

    type mismatch cannot convert from element type object to string

  6. 6

    Type mismatch: cannot convert from element type Object to Cookie

  7. 7

    Java - Type mismatch: cannot convert from element type Object to String

  8. 8

    Type mismatch: cannot convert from element type Object to List

  9. 9

    Java generics. Type mismatch: cannot convert from object to

  10. 10

    Type mismatch: cannot convert from Optional<Object> to BasketDTO

  11. 11

    spark Type mismatch: cannot convert from JavaRDD<Object> to JavaRDD<String>

  12. 12

    error : Type mismatch: cannot convert from Object to JSONObject

  13. 13

    Java generics. Type mismatch: cannot convert from object to

  14. 14

    Type mismatch: cannot convert from Set<Object> to Set<Long>

  15. 15

    Type mismatch: cannot convert from Class<capture#1-of ?> to Class<?>[]

  16. 16

    Type mismatch: cannot convert from Class<Parameterized> to Class<? extends Runner>

  17. 17

    issue with java 8 collectors Type mismatch: cannot convert from List<Object> to List<String>

  18. 18

    Type mismatch on java and cannot instantiate the type of object

  19. 19

    How can I fix (The left-hand side of an assignment must be a variable) and (Type mismatch: cannot convert from Object to String) in this code?

  20. 20

    Type mismatch: cannot convert from boolean to int

  21. 21

    Type mismatch: cannot convert from ListFragment to Fragment

  22. 22

    Type mismatch: cannot convert from long to int

  23. 23

    type mismatch: cannot convert from double to Double

  24. 24

    Type mismatch cannot convert from String to String[]

  25. 25

    Type mismatch: cannot convert from Scanner to boolean

  26. 26

    Type mismatch: cannot convert from void to Integer

  27. 27

    Type mismatch: cannot convert from int to TextView

  28. 28

    Type mismatch: cannot convert from void to int

  29. 29

    Type mismatch : cannot convert from double[][] to double[]

HotTag

Archive