0

I am writting a program that performs an a star search throughout a map. I have created a class that hold all the nodes of the map.

public Node {
   Node up_node, right_node, down_node, left_node;
}

public class Star {
    public static void main(String args[]) {
        Node a=new Node();
        Node b=new Node();
        Node h=new Node();
        Node here=new Node();

        Node[] NextNode;
        NextNode = new Node[10];
        for(int i=0;i<10;i++) {
            NextNode[i]=new Node();
        }
        int j=0;
        a.up_node=h;
        a.right_node=b;

        b.left_node=a;

        h.down_node=a;

        //if certain conditions are met
        NextNode[j].here_node=a.up_node;
        //what i was hoping to do is copy the node a.up which is h
    }
}

into NextNode[0] in this case. However it keeps returning a memory address of some sort : test.Node@10b28f30: test being the name of the package, please help!

4
  • 1
    returning where ? If you try to system.out.println the Node object you will sure end up with the Node@XXXX unless you override the toString method of the class node. Commented Apr 8, 2013 at 19:29
  • Apart from the other Node's references What value will be contained within each node? If you have that value then within toString() method return the String format of that value. This will solve your problem Commented Apr 8, 2013 at 19:29
  • 1
    "It keeps returning" is very vague. It's not clear where you're seeing that at all, but it's almost certainly the result of calling toString() on a Node - and you haven't overridden that method... Commented Apr 8, 2013 at 19:29
  • Quote:" NextNode[j].here_node=a.up_node;" here_node does not exists on the Node class? Commented Apr 8, 2013 at 19:31

3 Answers 3

1

@override the toString() method to display the internal property of your class.

By default, java display the full class name@hashCode value.

Sign up to request clarification or add additional context in comments.

Comments

1

Variables in Java are object references not actual objects. NextNode[j].here_node = a.up_node; will make NextNode[j].here_node and a.up_node point to the same object. Is this not what you want?

If you wanted to make a completely new copy of the object, then you can implement that in the Node class:

public class Node {
  Node up_node, right_node, down_node, left_node;

  public Node clone() {
    Node ret = new Node();

    // copy the properties
    ret.up_node = this.up_node;
    ...

    return ret;
  }
}

Now

NextNode[j].here_node = a.up_node.clone();

will make a copy (although it's only a shallow one -- the copy will point to the same objects via its fields as opposed to copies of them).

I assume your confusion about the code returning "an address" comes because you tried to print a node, e.g.

System.out.println(a.up_node);

You'll get something like test.Node@10b28f30, but try

System.out.println(NextNode[j].here_node);

and you should get exactly the same string, indicating that they're pointing to the same object.

To get something nicer, you must override Node's implementation of toString(). Here's an example that will give each Node a unique number:

public class Node {
  Node up_node, right_node, down_node, left_node; 

  // how many nodes were created
  private static int count = 0;

  // the number of this node
  private int number;

  public Node() {
    // increment the number of nodes created
    ++Node.count;
    // assign that number to this node
    this.number = Node.count;
  }

  public String toString() {
    return "Node #" + this.number;
  }
}

Comments

0

We know that every class that we write are child of Object class. When we print a child of an Object it prints its toString() method. By default it is a hashed value of memory location. So it prints sort weird things. if we @overriding toString method to return something more meaningful to us then we can solve this problem. If we can name our node class someway I think we can keep track of them easily

class Node(){
      String nameOfNode;    
      //contractor to keep track of where it goes. 
      public Node(String a){
             nameOfNode=a;

      }
      //when we will print a Node it is going to print its name
      public String toString(){
              return nameOfNode;
      }
}

Then it will print the name of the node. And it will stop showing that weird memory address.

and replace your new Node() with distinct name new Node("a name")

Comments

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.