System.arraycopy method to achieve copy of the array

1-1: System provides a native static method arraycopy (), you can use this method to achieve replication between arrays. For ordinary one-dimensional array, the values ​​will be replicated in each array to another array, where each element is passed by value, modify the copy will not affect the original value. And exemplary method Prototype Copy Copy primitive array as follows:

/**

 * System.arraycopy method Prototype

 * @Param src want to copy the source array

 * @Param srcPos source array to replicate starting position (starting from 0)

 * @Param dest target array to copy

 * @Param destPos start position of the target array (starting from 0)

 * @Param length length to be copied

*/

public static native void arraycopy(Object src,  int  srcPos,

            Object dest, int destPos, int length);

public static void main(String[] args) {

    int[] nums = {1024, 1025, 1026, 1027, 1028};

    int[] copyOfNums = new int[nums.length];

    System.arraycopy(nums, 0, copyOfNums, 0, nums.length);

    // modify elements of the array copy

    copyOfNums[3] = 1234;

    // observe whether the change in the original array

    System.out.println(Arrays.toString(nums));

}

Output: [1024, 1025, 1026, 1027, 1028]
because when you copy plain array, passed by value, the value of each element will copy to the new array, so modify the copy will not affect the original value.

 

 

An array of strings is more special, and look at the code:

String [] names = { "Zhaojun", "mermaid", "Yuanyuan", "Yang Yuhuan", "Su Daji"};

System.out.println ( "primary array values, each hash code:");

for(String name : names) {

    System.out.print(Integer.toHexString(name.hashCode()) + ", ");

}

String[] copyOfNames = new String[names.length];

System.arraycopy(names, 0, copyOfNames, 0, names.length);

copyOfNames [1] = "Luo Zhenshi";

System.out.println ( "\ n copy of each element of the array hash code, a hash code element 1 has changed:");

for(String name : copyOfNames) {

    System.out.print(Integer.toHexString(name.hashCode()) + ", ");

}

System.out.println();

// original array modified content copyOfNames elements have not changed

System.out.println(Arrays.toString(names));

After running the following results:

Each element of the original array hash code:

1be7059, 225f8ec, 23f0508, 1929eae, 1f6458e,

Copy each element of the array hash code, No. 1 element of the hash code has changed:

1be7059, 336eea6e, 23f0508, 1929eae, 1f6458e,

[Zhaojun, mermaid, Yuanyuan, jade bracelet, Su Daji]

Memory is as follows:

 

1-2: Copy array of objects

// entity class

public class Beauty {

    private String name;

    private int level;

    private double face;

 

    public Beauty() {}

 

    public Beauty(String name, int level, double face) {

        this.setName(name);

        this.setLevel(level);

        this.setFace(face);

    }

 

    @Override

    public String toString() {

        return name + ", " + level + ", " + face;

    }

    //省略 getters/setters

}

//testing method

public class ArraycopyDemo {

    public static void main(String[] args) {

        Beauty[] beauties = new Beauty[5];

        beauties [0] = new Beauty ( "Zhaojun", 5, 86.25);

        beauties [1] = new Beauty ( "mermaid", 6, 76.25);

        beauties [2] = new Beauty ( "Yuanyuan", 7, 56.25);

        beauties [3] = new Beauty ( "Yang Yuhuan", 8, 66.25);

        beauties [4] = new Beauty ( "Su Daji", 9, 96.25);

        Beauty[] newBeauties = new Beauty[beauties.length];

        System.arraycopy(beauties, 0, newBeauties, 0, beauties.length);

        // copy the modified array element property

        newBeauties [1] .setName ( "Luo Zhenshi");

 

        // print the contents of the original array, observation No. 1 element name attribute has been modified

        for(Beauty beauty : beauties) {

            System.out.println(beauty);

        }      

    }

}

operation result:

 

 Memory works as follows:

Conclusions:
1, when the array is one-dimensional array, and the element type is a primitive type or String, are deep copy, ie, the elements of the original array to the new array will not affect each other
2, when the array is a multidimensional array, or a dimensional array of elements is a reference type, belonging to shallow copy, the new elements of the original array with the array of references to the same object
impact here that, corresponding to the two array elements to copy, not necessarily the index is assigned to
a String special because of its immutability
multidimensional arrays can actually be understood as a dimension of each element in the array is the first address of a one-dimensional or multi-dimensional array, the result of the effect of the array of objects when the copy is the same

Dry notes more attention to micro-channel public number: the old nine school

Guess you like

Origin www.cnblogs.com/ljxt/p/11612967.html