Yes, you can use a temporary array that can hold all of the elements of your two arrays. Sort the temporary array and then copy this array back to the original arrays. Look at the java.util.Arrays class. It has both sorting and copying routines.
posted 9 years ago
thank you, Tom
i have looked in that class. but i dont find any answer.
will you please provide me an example for copying two array elements togther?
thank you again.
posted 9 years ago
Sorry, you can't afford me. I will tell you the following:
1. To create the temporary array, you can use Arrays.copyOf(). This method can also initialize it to one of your original arrays.
2. Copy the second original array by hand to the temporary array.
3. Use Arrays.Sort() to sort the array.
4. You can either use Array.copyOf() to create new arrays and assign them to the original variables or copy the sorted array back to the original arrays.
Do you know the reason why the designers put it in the System class and not in the Arrays class?
java.lang.System is from pre-Java 1.0; java.util.Arrays didn't appear in JDK 1.2 . So at the time, there was no such choice to be made. In hindsight, Arrays looks like the place for it, of course, and a forwarding wrapper for System.arraycopy() seems like it would be a good idea for Arrays.
I'm pretty sure, by the way, that Amit doesn't want to sort one long array with all the contents of two arrays; I think he has two parallel arrays (i.e., names and ages) and wants to sort them so the correspondence isn't broken. If that's the case, Amit, the sensible thing to do is to define a class -i.e., "Person" -- to hold the data items from all the arrays, and then sort an single array of Person, which will of course keep the data together. If you really need to, you could then copy the values back into the separate arrays, but really parallel arrays like that are always a bad design choice.