Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Java

Reply
 
LinkBack Thread Tools Display Modes
Old 03-14-2005, 11:38 AM   #1 (permalink)
plague
Registered User
 
Join Date: Mar 2005
Posts: 1
plague is on a distinguished road
Sorting an array of any Comparable objects

I've been searching all over the internet for this to no avail, so thanks in advance for any help anyone can give me. Basically I have to write a class that contains a sort method which takes in an array of any Comparable Objects and returns a new array of the same type without altering the original. It has to be able to do something like this:

sortedArray = SortClass.sort(unsortedArray);

where the arrays are separate and of any type of Object. I imagine I need to create a new array and clone each object, but the problem I'm having is how do I make the new array and cast the objects to the same type as the array that was passed in? Would it be something like:

public static Object[] sort(Object[] toBeSorted) {...}

or will I have to make use of the Class class, using Class<T>? I don't really know how to do this, I was just looking around on the API spec and this seems useful.

Thanks again.
plague is offline   Reply With Quote
Old 03-14-2005, 06:24 PM   #2 (permalink)
Belisarius
Java fanboy
 
Belisarius's Avatar
 
Join Date: Aug 2003
Posts: 1,139
Belisarius is on a distinguished road
You know that it's Comparables coming in, so you might as well return a Comparable array. The end user can cast it back. I haven't played around with the advancements with auto-casting in Java 1.5 though, so there might be a more efficient way to do it now.

The problem with cloning is that objects aren't required to implement it. For instance, BigDecimal is a Comparable, but it doesn't implement cloning. Therefore you aren't able to clone an array of them.

Now, you could create a new array and return it full of pointers to the objects in the other array, at which point you've fulfilled the letter of the assignment. You'll be returning a new array that affect the original. The catch is that any alterations to an object in the new array will affect the old one. But your assignment kinda sucks in that you can't ensure that the array coming in contains Cloneable objects.

As for generics and casting, I'd look at this PDF (it's small). I think you'll find Chapter 5 to have what you're looking for.

Let me know if you're still having problems.
__________________
GitS
Belisarius is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Text file input to an array letsC Standard C, C++ 9 03-06-2005 09:30 AM
Simple reverse programm silex Standard C, C++ 3 01-22-2005 08:03 AM
sorting objects arrays by object properties sde Java 28 08-05-2004 05:51 AM


All times are GMT -8. The time now is 01:03 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting