Results 1 to 5 of 5
  1. #1
    Arsenic is offline Member
    Join Date
    Mar 2009
    Location
    Atlantis
    Posts
    8
    Rep Power
    0

    Default Sorting ANY Object, and new Exceptions

    So I wrote this program to mimic the Array class. I've created a class called Manager.java which just holds the names, starting date, and pay of managers. In this class I've implemented compareTo().
    I also have coded an employee class which does not contain the Comparable interface, because I wanted to try out the exception class I wrote called ObjectNotComparableException.
    I ran into a bit of a snag when I came across a compiling error, and I'm not too sure what it means.

    Note: SortAnything.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.

    and when I compiled with -Xlint I get:

    SortAnything.java:26: warning: [unchecked] unchecked call to compareTo(T) as a member of the raw type java.lang.Comparable
    if (temp.compareTo(sortCopy[k+1]) < 0)
    ^
    1 warning


    I haven't really dealt with a language that uses cast a much as Java does, so when it came to making the generic sorting routine I was a bit confused.
    I realize "Comparable temp = (Comparable)sortCopy[k];" casts even a non-comparable object to be tagged as comparable, but I can't see any other way to use the compareTo() method within the sort if I don't use this line.

    Any help would be much appreciated.



    The sort code is below:

    Java Code:
    public class SortAnything
    {
    
    	public static Object[] sort(Object[] toSort)
    	{
    	
    	Object[] sortCopy = toSort.clone();
    	
    		try{
    		
    			for (int i = 0; i < sortCopy.length-1; i++)
    			{
    				for (int k = 0; k < sortCopy.length-1-i; k++)
    				{
    					Comparable temp = (Comparable)sortCopy[k];
    
    					if (temp.compareTo(sortCopy[k+1]) < 0)
    					{
    						sortCopy[k] = sortCopy[k+1];
    						sortCopy[k+1] = temp;
    					}
    				}
    			}
    		}
    		
    		catch(ObjectNotComparableException exp)
    		{
    			System.out.println("Object not comparable");
    		}
    		
    		finally
    		{
    			System.out.println();
    			System.out.println("Exiting sort().");
    			System.out.println();
    		}
    		
    		return sortCopy;
    	}
    		
    }

  2. #2
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    your class should implement the Comparable interface. If you aren't familiar with them, go read up on Comparable or interfaces.

    Comparable (Java 2 Platform SE v1.4.2)
    Interfaces (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)

  3. #3
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    Also, you might wanna look at using generics so you don't use Object (instead, use something like <T extends Comparable> or even just <Comparable>).

    Generics

  4. #4
    Arsenic is offline Member
    Join Date
    Mar 2009
    Location
    Atlantis
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by emceenugget View Post
    your class should implement the Comparable interface.
    I don't understand why sort would need to implement Comparable. There's nothing in sort's private data that would allow it to compare two instances. Note I have already mentioned that the one Manager class implements the Comparable class, with the compareTo() method. I've tested the sortAnything class, and it works fine with the Manager.java. When I try and test it with the non-comparable-implemented class, the exception isn't caught.

    Perhaps I should try if(toSort instanceof Comparable) before the sorting routine.

  5. #5
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    The objects you are sorting should implement Comparable, not your "SortAnything" class. If they cannot be compared, then there is no point in sorting them. If you find yourself casting in Java, you need to learn how to use Generics, which I indicated in my previous post.

Similar Threads

  1. Need Help With Exceptions
    By maggie_2 in forum New To Java
    Replies: 5
    Last Post: 12-15-2008, 07:12 PM
  2. URGENT: Sorting a vector of object by an element
    By doobybug in forum New To Java
    Replies: 1
    Last Post: 03-12-2008, 06:37 PM
  3. question regarding exceptions..
    By SCS17 in forum New To Java
    Replies: 3
    Last Post: 11-17-2007, 09:31 AM
  4. Runtime Exceptions
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-12-2007, 10:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •