java - Arrays.sort( myarray) physically changes the array? -


this question has answer here:

please me understand going on here:

main.java:

int[] myarray = new int[125]; // setup array here, code omitted.   int minimum = processarray.min(myarray );   // array values print if have been sorted... for(int i=0; i<myarray.length;i++) {    system.out.println(myarray[i]); } 

processarray.java

import java.util.arrays;  public class processarray {   public static int min(int[] anarray){      arrays.sort(anarray);      return anarray[0];   }   } 

after finding minimum value of array, array print in sorted order.

why array re-arranged in sorted order?

when pass array function reference myarray not clone/copy

your minimum function sorted array, sorted after call

you can clone array manually keep original array

int minimum = processarray.min(myarray.clone()); or use arrays.copyof(..)

keep in mind clone/copy "shallow copy" if array of objects objects still same...

take @ question is java "pass-by-reference" or "pass-by-value"?


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -