The procedure is as follows:
Class c{
public static void Main (String args[]) {
int a[][] = {{1,2,3},{4,5,6}};
int b[][] = new Int[a.length][a[0].length];
System.arraycopy (a,0,b,0,a.length); Copy an array via the arraycopy () function
B[0][0] = 4; Change the value of an array b[0][0]
System.out.println ("a[][]");
for (int i=0;i<2;i++) {
for (int j=0;j<3;j++) {
System.out.print (a[i][j]+ "");
}
System.out.println ();
}
System.out.println ("b[][]");
for (int i=0;i<2;i++) {
for (int j=0;j<3;j++) {
System.out.print (b[i][j]+ "");
}
System.out.println ();
}
}
The results of the printing are as follows:
A[][]
4 2 3
4 5 6
B[][]
4 2 3
4 5 6
If the two-dimensional array in the above program is changed to one-dimensional array, the result is different. The procedure is as follows:
Class c{
public static void Main (String args[]) {
int a[] = {1,2,3};
int b[] = new Int[a.length];
System.arraycopy (a,0,b,0,a.length); Copy an array via the arraycopy () function
B[0] = 4; Change the value of an array b[0]
System.out.println ("a[]:");
for (int i=0;i<3;i++) {
System.out.print (A[i] + "");
}
System.out.println ();
System.out.println ("b[]:");
for (int i=0;i<3;i++) {
System.out.print (B[i] + "");
}
}
}
Print the results as follows:
A[]:
1 2 3
B[]:
4 2 3
In the first program, with b[0][0] = 4; Only the value of the array b[0][0] is changed, but the result is that the value of the array a[0][0] has also changed. In the second program, because it is a one-dimensional array, changing the value of the b[0], the value of a[0] is not affected, so the problem may be on the dimensions of the array. A in the first program is an array of arrays (there is no concept of multidimensional arrays in Java, only an array of arrays), and likewise B, then a contains two elements, the first is a reference to the array (1,2,3), and the second is a reference to the array (4,5,6), and Arraycopy is to copy the contents of the array to the past, so the content of a (2 references) is copy to B, so you make changes to b[0][0, and a will change as well.
In Java, you can use the copy statement "A=b" to pass values to the data of the base type, but if a, B is two of the same type of array, replication is equivalent to passing a reference to an array variable to another array, and if an array changes, the variable referencing the same array will also change.
Method of copying an array element value in Java refers to a deep copy
1 copy each element of the array using a For loop (you need to invoke the Clone method for each object to achieve true replication)
2 Use the Clone method to get the value of the array, not the reference
3 Using the System.arraycopy method
Attention:
1. The arraycopy efficiency is higher in the above method.
2. The above method of copying an array is only for one-dimensional arrays, for multidimensional arrays, to replicate the values of the array elements, rather than the reference, in each dimension.
3. Clones and arraycopy when replicating a two-dimensional array, they are shallow copies, i.e.
Object[][] AA;
object[][] bb = Aa.clone ();
or bb=system.arraycopy (AA,0,BB, 0, bb.length);
The
Boolean B1 = (Aa[i] = = Bb[i]); False
Boolean b2 = (aa[i][j] = = Bb[i][j]); True, the visible array element copies only the reference. The old and new arrays point to the same memory address (whether an array of objects or an array of primitive types).
/**
* The shallow copy of the array is the index group copy, only the address of the array is copied, the old and new array points to the same data
* A deep copy of an array, regardless of whether the data is a basic type or an object type.
* For arrays, the difference is that when an array element is assigned a value, the base type value is passed, and the object type is a reference pass.
*
*/
Object[] A = new object[]{"String", New Integer (1)};
Object[] B = A;
/**
* There are several methods for deep copy of an array:
* 1. Invoke clone
* 2. Call System.arraycopy
* Both of the above are equivalent to the basic type and object type data effects.
* 3. Copies each element of an array using a For loop. (Note invoke the Clone method)
*
*/
/*
* When the array data is the base type,
*/
int[] array = new int[]{0,1,2,3,4};
int[] copy = Array.clone (); 1. Copy data
System.out.println (copy.equals (array));
System.out.println (copy = = array);
for (int i = 0; i < copy.length; i++) {
System.out.print (copy[i]+ ",");
copy[i]++; 2. Change the contents of the new array data
System.out.print (copy[i]+ ",");
System.out.println (array[i]+ ","); 3. Do not affect the original array
// }
System.out.println ();
/*
* When the array data is an object type,
*/
object[] src = new object[]{new String ("Zhao"),
Integer.valueof (1),
Integer.valueof (2),
Integer.valueof (3),
Integer.valueof (4)};
//
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.