first, there is a problem:
list<string> list = new arraylist<string> ();
List.add ("Zhangsan");
List.add ("Lisi");
SYSTEM.OUT.PRINTLN (list);
So what's the result of this printing?
[Zhangsan, Lisi]
So the question came, ' [] ' where it came from.
We know that all objects do not output the object's information until they have overridden the ToString () method of the object class. Otherwise the output would be something like cn.edu.lnu.test.box@15db9742, what it is. Is class ' All class name @hashcode ', we look at the source code of object:
Package Java.lang;
/**
* Class {@code Object} is the root of the class hierarchy.
* Every class has {@code Object} as a superclass. All objects,
* including arrays, implement the methods of this class.
* *
@author unascribed
* @see java.lang.Class *
@since JDK1.0
* * Public Class Object {public
native int hashcode ();
Public String toString () {return
getclass (). GetName () + "@" + integer.tohexstring (Hashcode ());
}
From this, we can conclude that a custom class can only print out the contents of an object only if it overrides the object's ToString () method.
So we guess: The list interface definitely rewrites the object's ToString (), and goes to see the source code and finds:
there is no ...
What the hell is going on?
look at the relationship of the collection in Java, the diagram above:
It turns out that Abstractcollection has rewritten the ToString () method. At this point, all the fog has been opened.