Java array replication method for copying arrays in java

Source: Internet
Author: User

Java array replication method for copying arrays in java

1. for Loop replication, but the efficiency is low

2. Reference, let the two arrays point to the same memory, but the isolation is poor, it is easy to change the value without knowing

3. Call the java method:

A. System. arraycopy (a1, 0, a2, 0, length)

A1 and a2 are two arrays. The first 0 indicates the starting point of a1, the second indicates the starting point of a2, and The length indicates the length to be copied.

B. a2 = Arrays. copyOf (a1, a. length)

Create an array copy


There are several methods for copying arrays in java.

The simplest one is to directly assign the value of the original array to the new array, but System is generally used. arraycopy (the number of the original array's start copy, the starting number of the new array's reception copy, and the value length)

There are several methods for copying arrays in java.

The simplest one is to directly assign the value of the original array to the new array, but System is generally used. arraycopy (the number of the original array's start copy, the starting number of the new array's reception copy, and the value length)

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.