Actually, the output of LinkedList’s toString() is like this
[111, 222, 333]
Why is this? Please look at the picture below:
From the picture, you can see that there is anAbstractCollection in its ancestor class, and in this class, there is an override oftoString():
public String toString() { Iterator it = iterator(); if (! it.hasNext()) return "[]"; StringBuilder sb = new StringBuilder(); sb.append('['); for (;;) { E e = it.next(); sb.append(e == this ? "(this Collection)" : e); if (! it.hasNext()) return sb.append(']').toString(); sb.append(',').append(' '); } }
What you are talking about is the default implementation of the Object class
Actually, the output of LinkedList’s toString() is like this
Why is this? Please look at the picture below:
From the picture, you can see that there is anAbstractCollection
in its ancestor class, and in this class, there is an override oftoString():
Seeing this, you should understand, right?
Why is mine different from yours...your code? JRE version?
java is designed like this, maybe you think
LinkedList
的toString
应该把所有Object
打印出来,但java设计者就认为只打印LinkedList
the short message itself is fine.You can write a function yourself to implement this function. It’s very simple, just loop it.