aspose file tools*
The moose likes Java in General and the fly likes Comparing Comparable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Comparing Comparable" Watch "Comparing Comparable" New topic
Author

Comparing Comparable

Fred Leaf
Greenhorn

Joined: Oct 09, 2011
Posts: 9
I'm doing my own Quicksort class and I've run into trouble (you knew that already since I wrote here). My problem is on line 20 where I in the while is doing a comparison between two Comparable objects. Eclipse tells me that "The method compareTo(capture#2-of ?) in the type Comparable<capture#2-of ?> is not applicable for the arguments (Comparable<capture#3-of ?>)". I guess what it means is that it thinks the two objects are of different classes. I (and any human) can easily see in the code that they are both the same class.
Am I on the right track on what the problem really is? Is there a way to convince ecplise that the two object I am to compare are of the same object? Is there some other workaround or do I have to solve it in another way?

Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Fred Leaf wrote:Is there a way to convince ecplise that the two object I am to compare are of the same object?

Sure, but you'll have to implement generics properly. If you don't want to go through that hurdle right now, I suspect removing all the <?>'s will work until you have the time, but I wouldn't leave it that way.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Well, you're using wildcards, so you're essentially saying: "I don't care what types these are exactly". The point is that you *do* care. You care that whatever the types are, they are the same. So you should use type parameters, not wildcards.

You also shouldn't be using a global field to do operations on during a sort. Two sorts are independent from each other, so they should perform operations on data that is local to the method. Pass the array to your helper method, don't store it in a static variable. Here's how two possible method signatures could look:
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4421
    
    8

Hi Fred. Welcome to The Ranch!

One other point about your code, not related to generics. You seem to be expecting that a.compareTo(b) will always return -1 if a is less than b. That's not true - all the definition of compareTo requires is that it returns a number less than 0. So check for that instead.
Fred Leaf
Greenhorn

Joined: Oct 09, 2011
Posts: 9
Stephan van Hulst wrote:Well, you're using wildcards, so you're essentially saying: "I don't care what types these are exactly". The point is that you *do* care. You care that whatever the types are, they are the same. So you should use type parameters, not wildcards.

You also shouldn't be using a global field to do operations on during a sort. Two sorts are independent from each other, so they should perform operations on data that is local to the method. Pass the array to your helper method, don't store it in a static variable.


Thanks for the input. Got me going in the right direction. The reason I'm not passing on the whole array to the helper method and instead has it as a class variable is out of memory concern (ok, I won't have problems with it, but using using 5-10 times as much memory when you don't have to just seems a waste to me). I just thought that passing the indexes along were more efficient. I thought about it a bit and then remembered that I'm doing a very similar thing anyway later on in the code where I'm sorting a list instead of an array. There, I'm passing on sublists. I'll have to consider more how I really want to implement it, both with arrays and with lists. I've adapted my code to your suggestion and I've taken a much deeper look into generics, but I havn't been able to get it to the exact place where I want it to be. I'll continue to try and get it there. Again, thanks for the input.

Matthew Brown wrote:Hi Fred. Welcome to The Ranch!

One other point about your code, not related to generics. You seem to be expecting that a.compareTo(b) will always return -1 if a is less than b. That's not true - all the definition of compareTo requires is that it returns a number less than 0. So check for that instead.


You are right about the return from compareTo. Used the correct way before, don't know why I changed it to equal instead of lesser then or equal. It would't help with the main issue though but this would certainly have caused problems later on. Thanks.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

There's no reason you have to worry about memory. In Java, everything is passed by reference. When you pass an array to a method, it passes a handle to the array object. It doesn't copy the array.

In the example I gave you, you could even use an in-place quick sort, which uses almost no additional memory other than the original array you're trying to sort. Note that you should decide and document whether your sorting methods will return a sorted copy of the original array, or whether they will just sort the original array itself.

In any case, there is no reason to store data globally.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Oh, I think I understand why you're worried about the memory. I assume you're talking about increase in memory when you pass a copy of a sub-list to the helper method.

Yes, to prevent the copy you have to access the original array, and pass indices for the range that needs to be sorted. However, you still don't have a reason to store the array in a static variable. You can pass the original array along with the range indices.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3647
    
  17

Alternatively, for a version that's easier to read you can do the same as you did before, except with non-static methods:
Fred Leaf
Greenhorn

Joined: Oct 09, 2011
Posts: 9
Yes, that was my worry. That suggestion of yours now defenatly seems like the way to go. I'll have to look more closely at it tomorrowor later during the week when I have more time. I'd prefer the static version of it. Fits better with other implementation I've done. Again, thanks for the input.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparing Comparable