我不明白为什么 arrSort
也会排序,即使我没有声明它已排序?
arrSort1 = arrSort
并不意味着 arrSort = arrSort1
对吧?
public static void main(String[] args) {
int[] arrSort = {4024, 4209, 9254, 8996, 9017, 6679, 3412, 6546, 2682, 42};
int[] arrSort1 = arrSort;
displayArray(arrSort);
displaySorted(arrSort1);
displayArray(arrSort);
}
public static void displayArray(int[] arrList){
for(int i = 0; i < arrList.length; i++)
System.out.print(arrList[i] + " ");
System.out.println();
}
public static void displaySorted(int[] arrSort1){
Arrays.sort(arrSort1);
for(int i = 0; i < arrSort1.length; i++){
System.out.print(arrSort1[i] + " ");
}
System.out.println();
}
输出显示
4024 4209 9254 8996 9017 6679 3412 6546 2682 42
42 2682 3412 4024 4209 6546 6679 8996 9017 9254
42 2682 3412 4024 4209 6546 6679 8996 9017 9254
最佳答案
arrSort1
和 arrSort
都引用同一个数组,因此结果,您可能需要使用 Arrays.copyOf
.
示例:
int[] arrSort1 = Arrays.copyOf(arrSort,arrSort.length);
关于Java 对两个整数数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43948057/