• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

about adding two arrays

 
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.
 
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.
 
author
Posts: 23836
140
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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.
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • 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.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!