如何使用 Java Arrays.sort

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7231042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:13:22  来源:igfitidea点击:

How to use Java Arrays.sort

javaarrayssorting

提问by czy

I am learning about how Arrays.sort(...)works in Java.

我正在学习如何Arrays.sort(...)在 Java 中工作。

Why are variables: tempand dopasboth sorted after the only sorting temp?

为什么变量是:temp并且dopas都在唯一排序之后排序temp

System.out.println("Before");
for (int i = 0; i < POP; i++)
  System.out.println(dopas[i]+"");           //dopas is unsorted

System.out.println("After");
float[] temp=dopas;
Arrays.sort(temp);                           //sort temp

for (int i = 0; i < POP; i++)
  System.out.println(temp[i]+" "+dopas[i]);  //Both temp and dopas are now sorted

I expected dopas to remain unsorted.

我希望多巴保持未分类。

回答by sleske

Arrays are objects in Java, therefore when using array variables you are actually using references to arrays.

数组是 Java 中的对象,因此在使用数组变量时,您实际上是在使用对数组的引用。

Thus the line

因此该行

float[] temp=dopas;

will only copy the referenceto array dopas. Afterwards, dopasand temppoint to the same array, hence both will appear sorted after using sort().

只会复制对 array的引用dopas。此后, dopastemp指向同一阵列;因此,这两会出现使用之后排序sort()

Use System.arrayCopyor Arrays.copyOfto create a copy of the array.

使用System.arrayCopyArrays.copyOf创建数组的副本。

回答by Dogmatixed

your assignment: float[] temp=dopas;is actually just copying a reference to the array. What I think you want to do is float[] temp = dopas.clone();

您的任务:float[] temp=dopas;实际上只是复制对数组的引用。我认为你想做的是float[] temp = dopas.clone();

回答by Jean Logeart

tempsimply is a reference to dopas. There actually is only one array in memory.

temp只是对dopas. 内存中实际上只有一个数组。

If you want tempto be a copy of dopas, try:

如果您想temp成为 的副本dopas,请尝试:

float[] temp = Arrays.copyOf(dopas, dopas.length);

This way, you will deep copyyour array instead of shallow copyingit!

这样,您将深复制您的数组而不是浅复制它!

回答by Oliver Charlesworth

Because in Java, you access arrays by-reference, not by-value.

因为在 Java 中,您是按引用访问数组,而不是按值访问数组。

So tempand dopasare both references to the same array.

所以tempdopas都是对同一个数组的引用。

回答by Stephen C

Why after Array.sort both arrays are sorted?

为什么在 Array.sort 之后对两个数组进行排序?

Because there is only one array. float[] temp=dopas;does not create a new array or copy the existing arrays contents. It simply copies an array reference... so that you have the reference to the same array in two places.

因为只有一个数组。 float[] temp=dopas;不会创建新数组或复制现有数组内容。它只是复制一个数组引用……这样你就可以在两个地方拥有对同一个数组的引用。

回答by pholser

When you assign tempthe value of dopas, it doesn't make a copy, it makes the variables refer to the same array.

当您分配temp的值时dopas,它不会进行复制,而是使变量引用同一个数组。

回答by Chris

Because temp and dopas (what we call a variable) are pointers to a space in memory. By using the code

因为 temp 和 dopas(我们称之为变量)是指向内存空间的指针。通过使用代码

float[] temp = dopas

you just say let "temp" point to the same space in memory as dopas does. So the result is that you have two pointers to the same space in memory. And by sorting temp you're sorting the contents of that space so by referencing dopas later in youre code you're accessing the exact same data.

你只是说让“temp”指向内存中与 dopas 相同的空间。所以结果是你有两个指向内存中相同空间的指针。通过对 temp 进行排序,您正在对该空间的内容进行排序,因此通过稍后在您的代码中引用 dopa,您将访问完全相同的数据。

PS: Dogmatixed mentioned a solution to your problem.

PS:Dogmatixed 提到了解决您问题的方法。

回答by ashutosh raina

temp is referencing dopas ,so when dopas gets sorted so does temp.They are in fact pointing to the same array.hope this helps.

temp 正在引用 dopas ,所以当 dopas 被排序时,temp 也是如此。它们实际上指向同一个数组。希望这会有所帮助。

回答by AlexR

Because both arrays temp and dopas are the same. Assignment temp=dopas;assigns reference to array. It does not copy its content. This is the reason why when you sort the first you sort the second too.

因为两个阵列 temp 和 dopas 是相同的。赋值temp=dopas;分配对数组的引用。它不会复制其内容。这就是为什么当你对第一个排序时你也对第二个排序的原因。

回答by Gabriel Negut

dopasand temprefer to the same array and can be used interchangeably.

dopas并且temp指的是同一个数组并且可以互换使用。

float[] temp=dopas;

After this line, temp[i]will be exactly the same as dopas[i].

在这一行之后,temp[i]将与dopas[i].