0

I'm new to java. unvisited and visited are linked lists with elements that are Node class objects, how can I make this method return a Node class object instead of an "Object"? I need to be able to use the output from this function to move onto the next unvisited puzzle state but I can't access the puzzle state within the Node class, and if I try to make the function return a Node it says it's a type mismatch (even though every single element in the list is a Node...). Please help, thank you.

/**
 * Removes last visited puzzle state from unvisited queue and adds it to the visited list. 
 * Returns next puzzle state to visit from unvisited queue. 
 */
public Object nextState() {
    // if unvisited is empty, return null (no solution)
    if (unvisited.size() == 0) { 
        return null;
    }
    // remove visited node from unvisited list & add to beginning of visited list
    visited.addFirst(unvisited.removeFirst());
    // visit next unvisited node
    Object first = unvisited.getFirst();
    return first;
}
3
  • Show us how you are doing your cast. Commented Sep 15, 2014 at 12:07
  • Return the datatype of unvisited before you cast it to object Commented Sep 15, 2014 at 12:07
  • Do you have problems with the return type of the linked list? Show us how you initialize unvisited. Commented Sep 15, 2014 at 12:13

2 Answers 2

1

change the return type of the function to Node instead of object like so

public Node nextState() {
// if unvisited is empty, return null (no solution)
    if (unvisited.size() == 0) { 
        return null;
    }
    // remove visited node from unvisited list & add to beginning of visited list
    visited.addFirst(unvisited.removeFirst());
    // visit next unvisited node
    Node first = unvisited.getFirst();
    return first;
Sign up to request clarification or add additional context in comments.

2 Comments

unvisited.getFirst() still gives me a type mismatch that it can't convert from Object to Node with this code...any other ideas?
Can you post all of your code so i can see everything that is there so i can assist you further
0
public Node nextState() {
// if unvisited is empty, return null (no solution)
    if (unvisited.size() == 0) { 
        return null;
    }
    // remove visited node from unvisited list & add to beginning of visited list
    visited.addFirst(unvisited.removeFirst());
    // visit next unvisited node
    Object first = unvisited.getFirst();
    if(first instance of Node){
        return (Node) first;
    }
    return null;
}

The problem is your unvisited.getFirst() is returning an object of type Object. So you need to cast it to Node type and return it. 'instance of' is a preventive measure, you can avoid it if you think it always returns an objet of type Node

4 Comments

If you set the generic type to Node, it should return a Node, not an Object, dosen't it? LinkedList<Node> unvisited = new LinkedList<Node> should fix it
Sorry, forgot the method brackets and can't edit my post... LinkedList<Node> unvisited = new LinkedList<Node>() should fix it
@Maze that was not in OP's question. So did not refer to that solution. Its great since it worked
@pratim_b Searched for the best solution and casting is not really the best. His question was what will solve his type mismatch, so it is a solution. Btw, it won't work because instanceof operator is one word.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.