GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes about adding two arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "about adding two arrays" Watch "about adding two arrays" New topic
Author

about adding two arrays

amit daundkar
Ranch Hand

Joined: Jan 24, 2009
Posts: 47
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

Joined: Jun 01, 2010
Posts: 618
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

Joined: Jan 24, 2009
Posts: 47
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

Joined: Jun 01, 2010
Posts: 618
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
Sheriff

Joined: Sep 28, 2004
Posts: 18876
    
  40


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

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Tom Reilly
Rancher

Joined: Jun 01, 2010
Posts: 618
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about adding two arrays