简体   繁体   中英

how can I make a copy of an array such that any change in the copy doesn't affect the original one?

I need to call an array each time in a loop and use it as an input in another method. then from the output of that method I will need to update the copy of that array and store the copy after change it in an ArrayList . like as an example:

double[]a={1,2,3,4}

after using ' a ' in a method as an input I will have to update ' a ' and store in the array list but next time in the loop I need the original ' a ' again. Once I do the following:

   double[]copya=a;
   copya[0]=10;

It changes the original 'a' as well. I need to have ' a ' like:

a={1,2,3,4}

even after updating the copy. Anyone can help me how I should fix this? Thanks.

When making copy , you have only created another reference variable and assigned it the same object as a . Now both a and copy refer to the same array.

To copy an array's contents, you can clone it.

double[] copy = a.clone();

It's a shallow copy, but for primitive values, that will work just fine.

Use Arrays.copyOf(double, int) .

Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length. For all indices that are valid in both the original array and the copy, the two arrays will contain identical values. For any indices that are valid in the copy but not the original, the copy will contain 0d. Such indices will exist if and only if the specified length is greater than that of the original array.

EDIT : As per your addition in the comments section of @Edward Samson's answer, below is a method that performs a "deep copy" of a 2D array:

public static double[][] deepCopy(double[][] original) {
    if (original == null) return null;

    double[][] result = new double[original.length][];
    for (int i = 0; i < original.length; i++) {
        System.arraycopy(original[i], 0, result[i], 0, original[i].length);
    }
    return result;
}

ORIGINAL ANSWER :

Alongside a.clone() and Arrays.copyOf(double, int) , you may also choose to use System.arrayCopy(Object src, int srcPos, Object dest, int destpos, int length) .

The Javadoc is below:

[System.arrayCopy] copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array. A subsequence of array components are copied from the source array referenced by src to the destination array referenced by dest. The number of components copied is equal to the length argument. The components at positions srcPos through srcPos+length-1 in the source array are copied into positions destPos through destPos+length-1, respectively, of the destination array.

Example usage might look like the following:

public static void main(String[] args)
{
        int[] first = {1, 2, 3, 4, 5, 6};
        int[] second = {0, 2, 4, 6, 8, 10};

        // Copy two elements from arr1 starting from the second element
        // into arr2 starting at the fourth element:
        System.arraycopy(
                        first,   // From the first array
                        1,       // Starting from the second element
                        second,  // To the second array
                        3,       // Starting at the fourth element
                        2        // Copy two elements
                        );
}

With the output (for second ) being {0, 2, 4, 2, 3, 10}

[Example derived from http://msdn.microsoft.com/en-us/library/aa989638(v=vs.80).aspx]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM