C # array deep copy,

Source: Internet
Author: User

C # array deep copy,

The array is deeply copied, that is, a new array is completely copied, and the content of the two arrays is identical.

There are generally four methods:

1. Loop traversal and Replication

2. array member method: CopyTo

The CopyTo method is used to copy all source arrays to the target array. You can specify the starting index of the target array. However, make sure that the target array can accommodate the lower source array. CopyTo can be used to merge multiple arrays.

3. Static Method of Array class: Array. Copy

Array. the Copy method can Copy some elements of the source array to the target array. When there are three parameters, you can specify the number of elements copied from the source array (starting from the first element ); you can specify not only the number of elements copied from the source array and the starting index, but also the starting index of the target array.

4. object class member method: Clone

Since the Clone return value type is object, it must be forcibly converted to int []

// Known array: int [] array = {1, 5, 9, 3, 7, 2, 8, 6, 4}; // (1 ). traverse and copy int [] copy1 = new int [array. length]; for (int I = 0; I <array. length; I ++) {copy1 [I] = array [I];} // (2 ). use the CopyTo method int [] copy2 = new int [array. length]; array. copyTo (copy2, 0); // (3 ). use Array. copy method int [] copy3 = new int [array. length]; Array. copy (array, copy3, array. length); // (4 ). use the Clone method int [] copy4 = (int []) array. clone ();

 

Related Article

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.