System.arraycopy use

Source: Internet
Author: User

When you look at Java code, you will often encounter this function system.arraycopy, for later convenience, summarized here. In Java, there are system.arraycopy using arraycopy instructions and using

The definition of arraycopy is as follows:

	public static native void Arraycopy (object src,  
										int  srcpos,
										object dest, 
										int destpos,
										int length);

This function copies the contents of Srcpos to srcpos+length-1 in the SRC source array to the destpos+length-1 in the dest target array. From the definition of a function, the function is a native function, so its implementation is not in the Java layer, so that it can make execution more efficient. The function parameters are described as follows:

SRC: Source Array

Srcpos: The starting position to copy the source array

Dest: Destination Array

Destpos: The starting position where the destination array is placed

Length: size of the Copy

In order to experience the use of this function, no more nonsense, directly on the code.

	private void Intcopydemo () {
		int[] Mcopysrcint = {1, 0, 0, 8, 6, 6, 8, 0, 0, 1};
		int[] Mcopydstint = new Int[mcopysrcint.length]; 
		
		SYSTEM.OUT.PRINTLN ("Destination array before arraycopy");
		for (int num:mcopydstint) {
			System.out.print ("" + num + ",");
		}
		System.out.println ();
		
		System.arraycopy (mcopysrcint, 0, mcopydstint, 0, mcopysrcint.length);
		
		SYSTEM.OUT.PRINTLN ("Destination array after Arraycopy");
		for (int num:mcopydstint) {
			System.out.print ("" + num +, ");
		}
	}
The results of the operation are as follows:

Destination array before arraycopy
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Destination array after arraycopy
1, 0, 0, 8, 6, 6, 8, 0, 0, 1,
arraycopy Use precautions

Although the use of arraycopy is very convenient, but there are some considerations.

1, the first guarantee that the copy and save the array is not empty, and copied content and saved content does not exceed the SRC and dest array, or will be an error. That's not nonsense.

2, as far as possible to ensure the type of SRC and dest consistency, the array of data best for the basic type, otherwise replication will cause the source array and target array shared memory, easy to cause errors.

Here is a two-dimensional array of examples to explain the next bar.

	private void Twodimensionalcopydemo () {string[][] mcopysrctwodmn = {"Asia", "I", "Guangdong", "Guangzhou"},
		{"Beijing", "Shanghai", "Guangzhou", "Shenzhen"}};
		
		string[][] mcopydsttwodmn = new String[mcopysrctwodmn.length][mcopysrctwodmn[0].length];
		System.out.println ("Before two-dimensional Copy"); for (int idx = 0; idx < mcopydsttwodmn.length, idx++) {for (String str:mcopydsttwodmn[idx)) {System.out.prin
			T ("" + str + ",");
		} System.out.println ();
		
		} system.arraycopy (mcopysrctwodmn, 0, mcopydsttwodmn, 0, mcopydsttwodmn.length);
		System.out.println ("\nafter two-dimensional arraycopy"); for (int idx = 0; idx < mcopydsttwodmn.length, idx++) {for (String str:mcopydsttwodmn[idx)) {System.out.prin
			T ("" + str + ",");
		} System.out.println (); }/*modify the destination array.
		* * Mcopydsttwodmn[1][0] = "Africa";
		
		MCOPYDSTTWODMN[1][2] = "Europe";
		System.out.println ("\nafter two-dimensional modify"); for (intIDX = 0; IDX < mcopysrctwodmn.length;
			idx++) {for (String Str:mcopysrctwodmn[idx]) {System.out.print ("" + str + ",");
		} System.out.println (); }
	}
The results of the operation are as follows:

Before two-dimensional Copy
NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL,


After two-dimensional arraycopy
Asia, Guangdong, Guangzhou,
Beijing, Shanghai, Guangzhou, Shenzhen,


After two-dimensional modify
Asia, Guangdong, Guangzhou,
Africa, Shanghai, Europe, Shenzhen,

Obviously, when you modify the target array, the source array changes as well. Why is this so?

Java actually does not have two-dimensional array concept, the ordinary implementation of two-dimensional array is only the element is one-dimensional array of one-dimensional array, and the array is also a reference type, inherited from the object class. The array is new. These properties also cause problems with arraycopy () two-dimensional arrays (this shows that similar problems can occur if an array of objects is present).
If it is a one-dimensional array, then the elements are basic types (such as int,double, etc.), using the Arraycopy () method, the value of the original array is passed to the new array, which belongs to the value pass. In the case of a two-dimensional array, the first dimension of the array is a reference to a one-dimensional array, and the second virial is the element value. After applying the Arraycopy () method to a two-dimensional array, the first-dimensional reference is copied to the first dimension of the new array, where the first dimension of the two arrays points to the same "those arrays". The value of the elements of any one of these arrays is changed, and the values of the elements of those arrays are modified, so the original array and the new array have the same element values.

If you need to complete the entire two-dimensional array of replication, but do not want to cause the above problems, you can use a one-dimensional array replication method.

		for (int idx = 0; idx < mcopysrctwodmn.length idx++) {
			system.arraycopy (MCOPYSRCTWODMN[IDX), 0, Mcopydsttwodmn[id X], 0, mcopydsttwodmn[0].length);
		

Well, that's the end of Arraycopy's use.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.