Yahoo Answers is shutting down on 4 May 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Singly Linked Lists with Strings?

Okay my program works correctly however when I run it I get an output that has the name of the package and some numbers in it like this:

[ hw4b.Node@45a877 hw4b.Node@1372a1a hw4b.Node@ad3ba4 hw4b.Node@126b249 hw4b.Node@182f0db hw4b.Node@192d342 ]

I have a toString method like this:

public String toString() {

Node curNode = head;

String info;

info = "[ ";

while(curNode != null) {

info += curNode.toString() + " ";

curNode = curNode.getNext();

}

info += " ]";

return info;

}

And my test class to test the output of the Nodes is:

private void run() {

MyLinkedList aList = new MyLinkedList();

String [] names = {"Frank", "Joe", "Sue", "Emily", "George", "Hillary"};

for(int i = 0; i < names.length; i++) {

aList.addLast(names[i]);

}

System.out.println("List after insertions: " + aList); }

I am not sure where the toString method comes into play or how it works to convert the nodes to strings. Please help :)

1 Answer

Relevance
  • Anonymous
    1 decade ago
    Favourite answer

    A toString() method of a class should return a value that represents an object of that class. An object inherits a default implementation for this method from the base class of all derived classes. However, as you see in your output, this default implementation isn't of much use. What you probably want is for the Node class to override this default implementation, and return simply the String that you store in the Node (i.e., the names that you store in the nodes). Your current toString() method seems to be a member of your linked list, not of the Nodes that make up the linked list.

Still have questions? Get answers by asking now.