Copy in java, deep copy, shallow copy (mark in advance)

clone

When brushing LeetCode today, because I forgot that the copying of reference type variables in java is to copy references, not to copy values, so I copied reference type variables just like before copying basic type variables.

results.add(result)

Here results are List<List<Integer>>, and results are List<Integer>. In the code, there is a backtracking process, which will continuously modify the result, and then make a judgment. After the judgment is successful, the result is added to the results.
So the final value in results is [result1,result2,result3,result4,result5,result6], here 1-6 is used to indicate that the value of results is different every time results are added.
But the result is wrong. I output the results before this line of code, and then finally output the results. The results are as follows
Insert picture description here
(the first 6 rows are constantly changing results, and the last row is results)
Every result is a value, but the result of the final results is empty. Why?

This writing is no problem for basic types, but it is different for reference types.
Because results are not as [result1,result2,result3,result4,result5,result6]simple as I thought , because they are reference types, what is added to results each time is not the value of result, but the reference of result, so the correct content of the result is [result,result,result,result,result,result]carried out in the overall code After the traceback, results==[], so results==[[],[],[],[],[],[]]. It can be explained in this way.

So when you add a reference type variable to the List multiple times, or whatever other containers, you must copy it, otherwise you will eventually find that the value added multiple times is only the last time, which is different from what you think. Although this is simple, it is often easy to forget, and once it is forgotten, it is fatal.

Modify the code here. Modify as follows

results.add((List<Integer>)result.clone());

Here is the use of clone, which is the method of object, used to copy the original object into a new object, so in this way, [result1,result2,result3,result4,result5,result6]the idea can be met .
In addition, there is one point to note here.

For List

List<xxxx> list= new xxxxxx<>();

It is not possible to use the clone method, and a similar error like clone is protected will be reported after use.
The clone method can be used with ArrayList or LinkedList

ArrayList<xxxx> list = new xxxxxx<>();

Need to pay attention here. (May need to be forced type conversion, because the online requirement may be List<>)

Change the code, the result is normal
Insert picture description here

Deep copy and shallow copy

After encountering this problem, I conducted a related query and found a fun thing, deep copy and shallow copy. Since I don’t need to touch this for the time being, I will summarize the blog I read and mark it in advance. I think I will encounter usage scenarios soon, and I will have to go back to this article to have a look and change it.

First of all, for copy, whether it is a deep copy or a shallow copy, it is a copy.
They all create a new object based on the original object. (Like our result.clone() above)

However, there is a difference between deep copy and shallow copy.
For example, the above ArrayList.clone() is a shallow copy. The shallow copy is that it only creates a new object A* based on the original object A, and if there is a reference type object b in the field of the original object, then the new object At the same location, b is also placed, and it does not create a new object b*.
The deep copy is the opposite, it creates a new b*.

Therefore, the difference between deep copy and shallow copy lies in the depth of the copy. One is that all objects in the object are copied, which is very deep. The other only considers the original object. Once the reference type is nested in the original object, it The new object and the original object are disconnected. This kind of copy can be understood as superficial or superficial.

Then, what is the serialization and what is the implementation of deep copy, I will add it later here.

Guess you like

Origin blog.csdn.net/qq_34687559/article/details/109091290