Java system.arraycopy

Source: Internet
Author: User

System.arraycopy method: If the array is larger, then using system.arraycopy will have an advantage, because it uses memory replication, eliminating a lot of time for array addressing access.

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

  Copies the specified source array src to the target array dest. copying begins with the Srcpos index of SRC , the number of copies is length, and the index copied to dest starts with Destpos.

1. For example

    @Test public    void Testcopy () {        int[] ids = {1, 2, 3, 4, 5};        1, the test copied to another array        //The IDs array index from 0 onwards after 5 number, copied to the IDS2 array index starting from 0        int[] ids2 = new Int[5];        System.arraycopy (IDs, 0, IDS2, 0, 5);        System.out.println (arrays.tostring (IDS2)); [1, 2, 3, 4, 5]        //2, test self-replicating        system.arraycopy (IDs, 0, IDs, 3, 2);        System.out.println (arrays.tostring (IDs)); [1, 2, 3, 1, 2]        //3, if the type conversion problem        object[] O1 = {1, 2, 3, 4.5, 6.7};        integer[] O2 = new integer[5];        try {            system.arraycopy (O1, 0, O2, 0, o1.length);        } catch (Arraystoreexception ex) {            //Storage conversion occurs, Partially successful data is copied past            System.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 remainder is not        System.out.println (arrays.tostring (O2));//[1, 2, 3, NULL, NULL]    }

  

2. After copying, change the copied array

, change the copied array and . But if you copy a , then change any of the arrays, then . Not very clear at the beginning, later on the Internet to check the information, understand the mystery.

java there is no concept of a two-dimensional array, the usual implementation of a two-dimensional array is simply a one-dimensional array of one-dimensional arrays, and arrays are reference types, inherited from the object class. The array is new. These properties also cause problems in arraycopy () two-dimensional arrays.

If it is a one-dimensional array, then the element is the underlying type (such as int,double, etc.), after using the Arraycopy () method, the value of the original array is passed to the new array. 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-dimension reference is copied to the first dimension of the new array, which means that the first dimension of the two arrays points to the same "those arrays." Changing the value of the elements of any of these arrays, in fact, modifies the values of the elements of "those arrays", so the elements of the original array and the new array are the same.

    @Test public void TestCopy2 () {int[] S1 = {1, 2, 3, 4, 5};        int[] s2 = new INT[5];        System.arraycopy (S1, 0, S2, 0, 5);        System.out.println ("This is S1");        for (int as1:s1) {System.out.print (aS1 + ",");        } s2[2] = 111;        System.out.println ("\nthis is S2");        for (int as2:s2) {System.out.print (aS2 + ",");        } System.out.println ("\nthis is S1");        for (int as1:s1) {System.out.print (aS1 + ",");        } System.out.println ("\ n-----------------------");        Two-dimensional array int[][] S3 = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}};        Int[][] S4 = new Int[s3.length][s3[0].length];        System.out.println ("This is S3");        System.arraycopy (S3, 0, S4, 0, S3.length); For (int[] as3:s3) {for (int j = 0; J < S4[0].length; J + +) {System.out.print (As3[j] + ","            );        }} S4[1][3] = 111; System.out.println("\nthis is S4"); For (int[] as4:s4) {for (int j = 0; J < S4[0].length; J + +) {System.out.print (As4[j] + ","            );        }} System.out.println ("\nthis is S3"); For (int[] as3:s3) {for (int j = 0; J < S4[0].length; J + +) {System.out.print (As3[j] + ","            ); }        }    }

Results: This is s1:1, 2, 3, 4, 5,
This is s2:1, 2, 111, 4, 5,
This is S1:1, 2, 3, 4, 5,
-----------------------
This is s3:1,2,3,4,5,6,7,8,9,10,
This is s4:1,2,3,4,5,6,7,8,111,10,

This is s3:1,2,3,4,5,6,7,8,111,10,

Java system.arraycopy

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.