Reprinted from: http://blog.csdn.net/java2000_net/article/details/4059465
System provides a native static method Arraycopy (), which we can use to implement replication between arrays. Its function prototypes are: public static void Arraycopy (object src, int srcpos, object dest, int destpos, int length) src: source array; Srcpos: source Array to copy The starting position; Dest: the destination array; Destpos: The starting position of the destination array placement; Length: How long the copy is. Note: Both SRC and dest must be of the same type or an array of types that can be converted. Interestingly, this function can implement itself to replicate itself.
Importjava.util.Arrays;/*** Lao Zi Zhu Java Improvement Tutorial-the use of the System.arraycopy method. <br> * <br> * Copy an array from the specified source array, copying from the specified position,<br> * to the end of the specified position in the destination array *@authorLao Zi's Home (java2000.net,laozizhu.com) **/ Public classlessionsystemarraycopy { Public Static voidMain (string[] args) {//This position is the native method. //Public static native void Arraycopy (//object src, int srcpos, Object dest,//int destpos, int length); //Initialize int[] ids = {1, 2, 3, 4, 5 }; System.out.println (arrays.tostring (IDs)); //[1, 2, 3, 4, 5]//Test self-replication//Copy 2 numbers starting at index 0 to the location indexed to 3System.arraycopy (IDs, 0, IDs, 3, 2); System.out.println (arrays.tostring (IDs)); //[1, 2, 3, 1, 2]//test copied to another array//3 data that starts with index 1 of the data is copied to the destination at index 0 int[] Ids2 =New int[6]; System.arraycopy (IDs,1, ids2, 0, 3); System.out.println (arrays.tostring (IDS2)); //[2, 3, 1, 0, 0, 0]//This feature requires//Source Start position + length cannot exceed end//target Start position + length cannot exceed end//and all parameters cannot be negative. Try{system.arraycopy (IDs,0, Ids2, 0, Ids.length + 1); } Catch(Indexoutofboundsexception ex) {//An out -of-bounds exception occurs and the data does not changeSystem.out.println ("Copy exception occurred: Data is out of bounds. "); } System.out.println (Arrays.tostring (IDS2)); //[2, 3, 1, 0, 0, 0]//If this is a type conversion problemObject[] O1 = {1, 2, 3, 4.5, 6.7 }; Integer[] O2=NewInteger[5]; System.out.println (Arrays.tostring (O2)); //[NULL, NULL , NULL, NULL, NULL] Try{system.arraycopy (O1,0, O2, 0, o1.length); } Catch(Arraystoreexception ex) {//A storage conversion occurs, and some of the successful data is copied overSystem.out.println ("Copy exception: Data conversion error, cannot be stored. "); } //as a result, the previous 3 data that can be copied are already stored. The rest is not.System.out.println (Arrays.tostring (O2));//[1, 2, 3, NULL, NULL] }}
[Java Basics] System.arraycopy use