toString Override in Java

xzibit72

New Member
This question is from an assignment. I have to override a toString() method in a class that creates a circularly linked list and I actually have a toString() method that works great, it passes all of my tests everything. So my project is autograded and it apparently doesn't approve of my method 100%. So my question is: is there a better way to write this toString() method that would be more efficient?\[code\]public String toString() { if (size == 0) { return "[]"; } else { String output = ""; Node<E> tempNode = actualElement; while (tempNode.next() != actualElement) { if (output.equals("")) { output = "[" + output + tempNode.data().toString(); tempNode = tempNode.next(); } else { output = output + ", " + tempNode.data().toString(); tempNode = tempNode.next(); } } output = output + ", " + tempNode.data().toString() + "]"; return output; }\[/code\]If i need to elaborate more on the class structure so that this makes more sense let me know.
 
Back
Top