Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

about adding two arrays

 
amit daundkar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello friends,

i have a question that given two arrays, how to sort them together.

what i think is to write a method to sort a single array with some algorithm like bubble sort.

please tell me how can we add two array elements together?

please provide an example.

thank you.
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
amit daundkar
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 20892
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The System.arraycopy() method is also good for copying elements between arrays.

Henry
 
Tom Reilly
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The System.arraycopy() method is also good for copying elements between arrays.

Excellent. Using System.arraycopy() obviates the need for any manual copying.

Do you know the reason why the designers put it in the System class and not in the Arrays class? The wild wild web suggests that it is a left over from the C language.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Reilly wrote:

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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic