Java Arrays.sort(test) sorts two arrays

后端 未结 1 580
一整个雨季
一整个雨季 2021-01-16 08:52

I have initiated an array in Java to be the same as another array. I have done this because I only want to sort a copy of the array not the original. This works fine the new

相关标签:
1条回答
  • 2021-01-16 09:36

    = is not used to copy elements of one array into another.

    Use

    double[] distancesSort = Arrays.copyOf(distances,distances.length);

    Arrays.copyOf(double[] arr, int length)

    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 0L. Such indices will exist if and only if the specified length is greater than that of the original array.

    Parameters:

    arr - the array to be copied
    length - the length of the copy to be returned

    Returns:

    a copy of the original array, truncated or padded with zeros to obtain the specified length

    If you use = then distancesSort will refer to distances. Any changes in distancesSort will reflect the changes in distances also.

    0 讨论(0)
提交回复
热议问题