Teen Programmers Unite  


Return to forum top

Sorting Arrays in Java

Posted by walker222 [send private reply] at March 06, 2003, 08:11:37 PM

I am trying to write a class, which I named ObjectSorter, which takes in an array of any object type and returns the sorted array, without altering the the original array which was passed in. I am using a static sort function to do the sorting. Here's my problem...I can't seem to figure out how to make a copy of the array that can be sorted and returned. I tried using:

Object[] SortedArray = (Object[]) array_of_objects.clone();

However, when I did this, I tried to return SortedArray after doing a selection sort on it, and it just returned the original array. So then I tried cloning each Object in the array bu doing....

Object[] SortedArray = new Object[arryofobjects.length]
SortedArray[i] = (Object)(arryofobjects[i]).clone();

However, this just told me that clone() was protected and that I could not use it. So any suggestions on how i could accomplish this? Thanks.

Posted by DragonWolf [send private reply] at March 07, 2003, 03:56:52 AM

I don't use arrays much (mostly use lists and hashmaps) does,

Object[] SortedArray = (Object[]) array_of_objects

not do what you want?

Posted by buzgub [send private reply] at March 07, 2003, 04:04:40 AM

DragonWolf, I think the problem is that that would make SortedArray merely a reference to array_of_objects - it wouldn't copy the data. To copy the data properly, and not just references to the data, you would need to use the (unavailable) clone method of the Object class.

As far as I can see, if you were happy to copy references to the objects in the array rather than the objects themselves you could create a new array the same size of the old one, and do new[i] = old[i]; for each element. If you want to actually have proper copies of the reference, then I can't think of any way other than playing with reflection and using it to see if the object has a usable clone method, and throw an exception if it doesn't. That's getting way beyond my level, though.

Posted by CViper [send private reply] at March 07, 2003, 06:12:33 AM

If you want to copy each object you can declare your own public copy function (dup() or whatever you want to call it):

public className dup()
    return new className( this ); 

Obviously this requires an constructor to be defined which takes the proper arguments etc.

I'm sure there are some better ways to do this though...
Posted by pramod [send private reply] at March 09, 2003, 10:19:49 PM

Objects which can really be cloned implement Cloneable. So you can do things like x instanceof Cloneable. That also means yu hae indvidually clone each object. clone is supposed to return a real, rather than a referenced copy. IMHO ArraySorter or whatever should work with Cloneables rather than Objects. I think clone is the Java equivalent of a copy constructor. But a copy constructor is not a bad idea either.

Posted by pramod [send private reply] at March 09, 2003, 10:20:37 PM

As an afterthought, you can create a new array and use ArrayCopy [Can't remember where that is. System?]

You must be logged in to post messages and see which you have already read.

Log on
Save for later automatic logon

Register as a new user
Copyright TPU 2002. See the Credits and About TPU for more information.