this is my first time on this board, so hi. :p
I'm currently learning basic java in college and I need some help.

I have a constructor for my Node class and I've linked 3 string nodes together and I need to print them in order. So far I got this:

for (Node node = node1; node != null; node = node.nextnode)
    {
        System.out.println(node);
    }

but that just prints out the memory address, how do I make it print out the string I assigned to them? thanks

Recommended Answers

Of course it will... use System.out.println(node.YOURSTRING);

Jump to Post

All 4 Replies

Of course it will... use System.out.println(node.YOURSTRING);

Of course it will... use System.out.println(node.YOURSTRING);

o wow that was simple, thanks!

Actually, If I may add, it does not print the memory address. That is the hashcode of the object.
When you call the System.out.println(anObject) what it actually does is call the toString method: System.out.println(anObject.toString()) Since you haven't overridden the toString method it uses the one from the super class the Object. Check the API.

If in your Noce class you had this it would work:

class Node {
  private String data;

  public String toString() {
     return data;
  }
}

is there an Edit button somewhere?
anyway, instead of spamming the forums with my questions, I decided to just update this one.
my question is:
without using ArrayList, how can I make a method to increase the size of an array by 1 every time it's called? I know that you can't just increase the size but need to create a new one.

thanks for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.