The following method is taken from an implementation of LinkedList. It is not an infinite looping recursion, even though the method seems to invoke itself.
@Override
public String toString() {
StringBuilder result = new StringBuilder("[");
Node<E> current = head;
for (int i = 0; i < size; i++) {
result.append(current.element);
current = current.next;
if (current != null) {
result.append(", ");
} else {
result.append("]");
}
}
return result.toString();
}
I presume that the penultimate line invokes a super.toString() method. But I feel insecure with this presumption. A confirmation or an alternative explanation would be appreciated.