Behavior of an array after sorting in Java -


I have to get some ideas on the behavior of the following programs:

  package head; Import java.util.Arrays; Public class stringgram {public static zero major (string agre []) {string a = "abba"; Four [] aArr = a.toCharArray (); Arrays.sort (aArr); // 1 Arrays.sort (a.toCharArray ()); // 2 System.out.println (aArr); // System.out.println (a.toCharArray ()); // unororted}}   

According to me there is a character array type referenced by aArr , but when it comes to statement 2, the character array is getting sorted But in some way the sorted array is not referred to by any variable, so the behavior is lost. Nobody can help me.

Yes you are right.

toCharArray () Each call actually creates a new array instance with characters in the string.

In the case of > AARA , you actually refer to the new array instance, you use aArr to sort its array examples Specified by variable aAr that gets resolved.

But when you pass a.toCharArray () to the Array.sort () method, you are passing the array frequency that you have Not to mention the variable is the array instance gets solved, but you do not have any reference.

When you have created a new array instance using println again a.toCharArray () and println Which is clearly not ordered

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -