开发者

How to use Java Arrays.sort

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

Why are variables: temp and dopas both sorted a开发者_开发百科fter the only sorting 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.


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

Thus the line

float[] temp=dopas;

will only copy the reference to array dopas. Afterwards, dopas and temp point to the same array, hence both will appear sorted after using sort().

Use System.arrayCopy or Arrays.copyOf to create a copy of the array.


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();


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

If you want temp to be a copy of dopas, try:

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

This way, you will deep copy your array instead of shallow copying it!


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

So temp and dopas are both references to the same array.


Why after Array.sort both arrays are sorted?

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.


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


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

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.

PS: Dogmatixed mentioned a solution to your problem.


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.


dopas and temp refer to the same array and can be used interchangeably.

float[] temp=dopas;

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


Because temp and dopas are two references to the same array


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 and dopas share the same pointer, since you don't create a new instance of array, you just assign the same pointer.


temp and dopas are referencing the same array.

if you want temp to act like a new array, then use clone() method.

float[] temp = dopas.clone();

clone() method creates a new array and copies the contents of the dopas to the newly created array.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜