ToString () method instance code in Java

preface:

The toString () method is believed to have been used by everyone. It is generally used to return the relevant data of the object in the form of string.

Recently, a collection in the form of ArrayList < ArrayList < integer > > data needs to be processed in the project.

The processing requires that the set data be converted into string form. The format is: subset 1 data + "#" + subset 2 data + "#" ++ Subset n data.

Example: set data: [1,2,3], [2,3,5]] requires conversion to a string in the form of "[1,3] #[2,5]"

For the first time:

Then look at the log under this process:

05-12 10:29:18.485 9565-9565/com. xxx. aaa I/myinfo: [1,2] 05-12 10:29:18.485 9565-9565/com. xxx. aaa I/myinfo: [2,3] 05-12 10:29:18.495 9565-9565/com. xxx. aaa I/myinfo: [1,3]

We will find that what we want is a string in the form of [1,3], but the result is [1,3]. After the second value starts, there is an extra space in front of it.

Next, let's look at the under the collection Source code of tostring() method:

Translate the official explanation:

1. Return the string representation of this collection class (the parent class of set and list)

2. This representation has a specified format, which is contained by the rectangular bracket "[]"

3. The child elements inside are separated by "," (comma and space) (this is the focus)

Analyze the data under this collection The toString () method source code is divided into several parts:

1. Judge whether the set is empty, that is, whether there is data in the set. If it is a null value (no data), the string "[]" is returned directly

2. If the collection is not null, there is data

① iteratively remove a child element (object next = it. Next()). If the child element is the collection itself, add "(this collection)" to the buffer object of StringBuffer class

②. If this sub element is not the collection itself, add it to the buffer object

③ if there are child elements below this child element, add "," to the buffer object to split two adjacent child elements

3. Return StringBuffer Tostring() string

It can be seen that the return [1,3] is the official correct return form. In fact, for this problem, it can be used after the obtained string without changing the source code replaceAll(" ",""); Remove all spaces from the string

Note: there is a code in the source code:

Some students may not understand it here. For example, let's add the code C. add (c) to the subset; Add the set itself to the set and see the print results

Look at the red part of the log results to see if you understand it. If the child element in the collection is the collection itself, add "(this collection)" to the returned collection

05-12 10:58:00.615 8424-8424/com. maiji. magkarepatient I/myinfo: [1,(this Collection)] 05-12 10:58:00.615 8424-8424/com. maiji. magkarepatient I/myinfo: [2,(this Collection)]  

So far, the above problem has been solved. Let's look at the problems under other classes Tostring() source code.

1、 Object

Translate the official explanation:

1. Return a concise and readable string for this object

2. Subclasses of the object class are encouraged to override this method to provide an implementation to describe the type and data of the object

3. The default execution form is consistent with the following example

To sum up: when you do not override a class This of the root class object will be executed when the toString () method is used Tostring() method.

Return form: hexadecimal of the class name + @ + hash value of the object

Examples:

05-12 11:23:00.758 17406-17406/com. maiji. magkarepatient I/myinfo: java. lang. Object@e23e786

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>