Detailed explanation of array copy (clone and array copy) code in Java
The copy of Java array is passed by reference, not the value of other languages.
1、clone
protectedObjectclone()
Throwsclonenotsupportedexception creates and returns a copy of this object. The exact meaning of "copy" may depend on the class of the object. The purpose of this is that for any object x, the expression:
x.clone()!= X is true, expression:
x.clone(). Getclass() = = X. getclass() is also true, but these requirements do not have to be met. Normally:
x.clone(). Equals (x) is true, but this is not a requirement that must be met.
By convention, the returned object should be returned by calling super Clone gets. If a class and all its superclasses (except object) comply with this Convention, x.clone() getClass()==x.getClass()。
By convention, The object returned by this method should be independent of the object (the object being copied). To obtain this independence, it is necessary to modify one or more fields of the object before super.clone returns the object. This usually means copying the internal "deep structure" containing the object being copied And replace references to these objects with references to copies. If a class contains only basic fields or references to immutable objects, there is usually no need to modify super The fields in the object returned by clone.
The clone method of the object class performs a specific copy operation. First, if the class of this object cannot implement the interface clonable, clonenotsupportedexception will be thrown. Note that all arrays are considered to implement the clonable interface. Otherwise, this method will create a new instance of the object's class and initialize all the fields of the object by strictly using the contents of the corresponding fields of the object as through allocation; The contents of these fields are not self copied. Therefore, this method performs a "shallow copy" of the object rather than a "deep copy" operation.
The object class itself does not implement the interface clonable, so calling the clone method on an object with class object will cause an exception to be thrown at runtime.
return:
A copy of this instance.
Throw:
Clonenotsupportedexception - if the object's class does not support the clonable interface, the subclass overriding the clone method will also throw this exception to indicate that an instance cannot be copied.
1. The clone method is used to create a copy of the object. In order to use the clone method, the class must implement Java Lang. clonable interface overrides the protected method clone,
If the clonebale interface is not implemented, clonenotsupportedexception will be thrown.
2. The constructor is not called when cloning Java objects.
3. Java provides a default method called shallow copy to implement clone. After creating a copy of the object, copy the content by assignment,
This means that if your class contains reference types, the original object and clone will point to the same reference content, which is very dangerous,
Because any changes that occur in variable fields will be reflected in the common content they reference. To avoid this situation, the referenced content needs to be deeply cloned.
2、arraycopy
publicstaticvoidarraycopy(Objectsrc,
intsrcPos,
Objectdest,
intdestPos,
Intlength) copies an array from the specified source array, starting at the specified location and ending at the specified location of the target array.
A subsequence of array components is copied from the source array referenced by SRC to the target array referenced by dest. The number of the copied component is equal to
Length parameter. Components in the source array between srcpos and srcpos + length-1 are copied to destpos in the target array respectively
To destpos + length-1.
If the parameters SRC and dest refer to the same array object, the copying process is like copying the components from srcpos to srcpos + length-1 to a temporary array with length components, and then copying the contents of the temporary array to destpos to destpos + length-1 of the target array.
If dest is null, a NullPointerException exception is thrown.
If SRC is null, a NullPointerException exception is thrown and the target array is not modified.
Otherwise, an arraystoreexception exception is thrown and the target array is not modified as long as any of the following conditions are true:
The SRC parameter refers to a non array object.
The dest parameter refers to a non array object.
SRC parameters and dest parameters refer to arrays whose component types are different basic types.
The SRC parameter refers to an array with a basic component type, and the dest parameter refers to an array with a reference component type.
The SRC parameter refers to an array with a reference component type, and the dest parameter refers to an array with a base component type.
Otherwise, as long as any of the following conditions is true, an indexoutofboundsexception will be thrown and the target array will not be modified:
The srcpos parameter is negative.
The destpos parameter is negative.
The length parameter is negative.
Srcpos + length is greater than Src Length is the length of the source array.
Destpos + length is greater than dest Length is the length of the target array.
Otherwise, if the actual component from srcpos to srcpos + length-1 in the source array can not be converted into the component of the target array through allocation conversion
Type, an arraystoreexception exception is thrown. In this case, K is set to the smallest non negative integer smaller than the length, so it cannot be used
Convert SRC [srcpos + k] to the component type of the target array; When an exception is thrown, from srcpos to the source array component at srcpos + k-1
It has been copied to the destpos + k-1 position in the target array, while other positions in the target array will not be modified.
(because of the limitations already detailed, this paragraph can only be effectively applied to
Both arrays have component types of reference type.)
Parameters:
SRC source array.
Srcpos - the starting position in the source array.
Dest - destination array.
Destpos - the starting position in the target data.
Length - the number of array elements to copy.
Throw:
Index out of boundary exception - if copied, it will lead to access to data outside the range of the array.
Arraystoreexception - if the elements in SRC array cannot be stored in dest array due to type mismatch.
NullPointerException - if SRC or dest is null.
3. Test code
Test results:
summary
The above is all about the detailed explanation of array replication (clone and array copy) code in Java. I hope it will be helpful for you to learn about array replication in Java.