0

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

3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by Phoenix_7
0

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

o wow that was simple, thanks!

0

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

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.