Home > Back-end > Why ArrayList directly output is not a memory address, but the array elements (toString output)
Why ArrayList directly output is not a memory address, but the array elements (toString output)
Time:12-18
CodePudding user response:
You look at the ArrayList ToString () of the source code, is to rewrite the ToString ()
CodePudding user response:
Behind the ArrayList is of type string, the output is not of type string...
CodePudding user response:
/* ** Returns a string representation of this collection. The string * representation consists of a list of the collection 's elements in the * the order they are returned by its iterator, enclosed in square brackets * (& lt; Tt>" [the] <""/tt>) . Adjacent elements are separated by the characters * & lt; Tt>" , "& lt;/tt> (comma and space). Elements are converted to strings as * by {@ link String# the valueOf (Object)}. * * @ return a string representation of this collection */ Public String toString () { Iterator It=the 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 ('] '). The toString (); Sb. Append (', '). Append ("); } }