• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Piet Souris
  • Carey Brown
  • Stephan van Hulst
Bartenders:
  • Frits Walraven
  • fred rosenberger
  • salvin francis

Comparing Comparable

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Saloon Keeper
Posts: 13075
281
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Bartender
Posts: 4568
9
  • Likes 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Saloon Keeper
Posts: 13075
281
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 13075
281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 13075
281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic