Array copy (system.arraycopy, deep copy)--array
"itjob Course Materials"
Copy Array
Once an array is created, its size is not adjustable. However, you can use the same reference variable to refer to a completely new array:
int[] MyArray = new int [6];
MyArray = new INT[10];
In this case, the first array is discarded unless other references to it remain elsewhere.
The Java programming language provides a special method of copying an array in the System class, which is called arraycopy (). For example, araycopy can be used as follows:
Int[] MyArray = {1, 2, 3, 4, 5, 6}; Original array
Int[] Hold = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; New larger array
System.arraycopy (MyArray, 0, hold, 0, myarray.length); No array copy all elements to hold, starting from subscript 0
At this point, the array hold has the following contents: 1,2,3,4,5,6,4,3,2,1.
Note: If the array holds the base type, copy the value directly. If the array holds reference types (class types, array types (copies of multidimensional arrays), etc.), then the address of the reference type is copied, see the following example:
Example 5
class AA {
int i;
Public AA (int II) {
i = II;
}
}
Public class testarraycopy {
Public Static void Main (string[] args) {
Declares an array and initializes the source array
AA str1[] = new aa[] { new AA (1), new AA (2), new AA (3), new AA (4)};
The destination array of the copy
AA str2[] = new aa[str1.length];
Full copy, array method parameter introduction look at the API
System. arraycopy (str1, 0, str2, 0, str1.length);
Changing the destination array
STR2[1].I = 5;
Prints the original array if there is no change to the description is two arrays
for (int i = 0; i < str1.length; i++) {
System. out. Print (str1[i].i + "");
}
}
}
Obviously, the array has changed, that is, after the copy operation, the original array and the new copy of the array are not separated, because the copy will be a reference to the element.
For multidimensional arrays, because the array itself is a reference type, its copy attribute is the same as the reference type array.
Example 6
Public class testmutipledemensionarraycopy {
Public Static void Main (string[] args) {
Defines an array of arrays
int [] Source = new int[5][];
Defining the destination array
int [] Target1 = new int[5][];
int [] Target2 = new int[5][];
Assigning a value to a source array
for (int i = 0; i < 5; i++) {
Source[i] = new int[i + 1];
int temp=i;
for (int j = 0; J < Source[i].length; J + +)
SOURCE[I][J] = j + 1;
}
Print data from the source array
System. out. println ("-------------source data-------------");
for (int i = 0; i < source.length; i++) {
for (int j = 0; J < Source[i].length; J + +)
System. out. Print (Source[i][j] + "");
System. out. println ();
}
Copy of array (shallow copy)
System. arraycopy (source, 0, Target1, 0, source.length);
Change the value of the destination 1 array
TARGET1[1][0] = 100;
Print source array information, you can see the value changes, indicating that there is no deep copy
System. out. println ("-----------light copy after output-----------");
for (int i = 0; i < source.length; i++) {
for (int j = 0; J < Source[i].length; J + +)
System. out. Print (Source[i][j] + "");
System. out. println ();
}
The deep copy of the array, first copy "first dimension"
System. arraycopy (source, 0, Target2, 0, source.length);
Deep Copy again
for (int i = 0; i < 5; i++) {
Target2[i] = new int[i + 1];
System. arraycopy (Source[i], 0, Target2[i], 0, i + 1);
}
Change the data of the destination 2 array
TARGET2[1][0] = 999;
Print the source array information, you can see that the value has not changed, the description is a deep copy
System. out. println ("-----------deep copy output does not change 100 to 999-----------");
for (int i = 0; i < source.length; i++) {
for (int j = 0; J < Source[i].length; J + +)
System. out. Print (Source[i][j] + "");
System. out. println ();
}
}
}
[reproduced from Baidu Library] array copy (system.arraycopy, deep copy)--array