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 g
Use StringBuilder
instead may do your a favor.This is snippet is copied from AbstractCollection.toString()
,take a look at it.
public String toString() {
Iterator i = iterator();
if (! i.hasNext())
return "[]";
StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
E e = i.next();
sb.append(e == this ? "(this Collection)" : e);
if (! i.hasNext())
return sb.append(']').toString();
sb.append(", ");
}
}