czy
czy

Reputation: 175

How to use Java Arrays.sort

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

Why are variables: temp and dopas both sorted after 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.

Upvotes: 9

Views: 11375

Answers (13)

ram914
ram914

Reputation: 331

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.

Upvotes: 0

sleske
sleske

Reputation: 83577

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.

Upvotes: 21

ashutosh raina
ashutosh raina

Reputation: 9314

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

Upvotes: 1

Jean Logeart
Jean Logeart

Reputation: 53809

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!

Upvotes: 6

Dogmatixed
Dogmatixed

Reputation: 794

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

Upvotes: 7

Chris
Chris

Reputation: 7855

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.

Upvotes: 2

Emmanuel Bourg
Emmanuel Bourg

Reputation: 10958

Because temp and dopas are two references to the same array

Upvotes: 1

Gabriel Negut
Gabriel Negut

Reputation: 13960

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].

Upvotes: 1

Cygnusx1
Cygnusx1

Reputation: 5409

temp and dopas share the same pointer, since you don't create a new instance of array, you just assign the same pointer.

Upvotes: 0

AlexR
AlexR

Reputation: 115328

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.

Upvotes: 1

Stephen C
Stephen C

Reputation: 718678

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.

Upvotes: 3

pholser
pholser

Reputation: 4937

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

Upvotes: 2

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272457

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

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

Upvotes: 3

Related Questions