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 (");
}
}

CodePudding user response:

The
reference 3 floor nayi_224 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 (");
}
}
why turn a string array, direct output is also a toString, string to an array of bytes is memory address... My good disorderly, this a few...

CodePudding user response:

Can directly output is this class overrides the toString method, print out the address is this class have to rewrite the toString method,

CodePudding user response:

The
refer to the original poster weixin_44455902 response:


Print the object is the tostring method invocation object itself, by default the tostring method is output address, some kind of rewriting the tostring method is used to output content, Arraylist is to rewrite the tostring method, so the output is the content of the Arraylist

CodePudding user response:


1, for this code:
 System. Out. Println (list); 

Want to see how he output, click println look at its source:
 public void print (Object obj) {
Write (String. The valueOf (obj));
}

Discovered by the String. The valueOf (obj) output, continue to point in:
 public static String the valueOf (Object obj) {
Return (obj==null)? "Null" : obj. ToString ();
}

Sout is found according to the object's toString method output, that couldn't go to see the ArrayList toString is how to rewrite,
2, ArrayList toString method source:
(1) find the ArrayList found itself not rewrite the toString method, but with the parent the parent class AbstractList AbstractCollection method
(2) the source code is as follows:
 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 (");
}
}

CodePudding user response:

See you in 4 # said: "why turn a string array, direct output is also a toString, string to an array of bytes is memory address... My good disorderly, this a few... "
How do you turn, can read the code sticking out,

CodePudding user response:

I feel he should want to say is why direct print the array output is memory address
With Arrays directly. In case you want to print directly array toString (arr) this method

CodePudding user response:

Override the tostring the bai

CodePudding user response:

Because of type String rewrite the toString method, so print is character, rather than an address

CodePudding user response:

Inherited AbstractList ArrayList, AbstractList class overrides the toString () method, so they will print the value, not address values

CodePudding user response:

The above said is not wrong ArrayList to the ArrayList inherited AbstractList toString method, AbstractList and inherited AbstractCollection is AbstractCollection abstract class overrides the toString method
  • Related