• 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
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Implementing Comparable and compareTo() method.

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am having trouble implementing a compareTo() method.

I am creating an ArrayedHeap that stores items of type I, where I is later defined.
For example ArrayedHeap<Integer> H = new ArrayedHeap280<Integer>(10); // 10 is simply the capacity

What I fail to comprehend is that when I am creating the compareTo(I item) method I dont know what I is, seeing as it is later defined.
So how I can I implement a compareTo method when I cant use operators such as < and > or <= and >=?

When I try what I have below, eclipse tells me "The operator > is undefined for the argument type(s)
ArrayedHeap280<I>, I"

Any help would be greatly appreciated.

My code is as follows:

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hello, there is no logic using > to compare two objects?! so your item should have a get-method that returns the value you want to compare.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch. Please UseCodeTags when you post source code.

So, you're creating a class that has a type parameter I, and now you want to do something with those I objects in your compareTo() method.

If you define your class like this:

then inside your class, you can't do much with objects of type I, because anything could be filled in for the I. You certainly can't compare instances of I with operators like >, because the compiler can't check that an operator > actually exists for objects of some unknown type I.

To be able to compare objects of type I, you have to specify constraints for the type parameter I. For example:

What this is expressing is: for I, you can use any type that implements the Comparable interface for I. By doing this, the compiler knows that compareTo() will always be available on I, so you can call it.

Have a look at this example:

Note: You'll never be able to use operators like < and > to compare instances of I; those operators are only defined for numeric primitive types (int, long, float, double, etc.) and cannot be used on objects.
 
Marshal
Posts: 76860
366
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch I have added cpde tags and you can see how much better your post looks.

The Comparable interface says it is for classes with a "natural ordering". It is obvious from the way you have written that the Item class has a "natural ordering". But what about the Heap class? How will that have a "natural ordering"? If you can't give a simple explanation, then maybe the Heap class doesn't have a "natural ordering" at all, and the Item class should implement Comparable<T>, not the Heap class.

By the way, Jesper, isn't it Heap<Comparable<T extends Comparable<? super T>>>? The compareTo method might be declared in a superclass of T.
 
Sheriff
Posts: 22701
129
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:By the way, Jesper, isn't it Heap<Comparable<T extends Comparable<? super T>>>? The compareTo method might be declared in a superclass of T.


With java.sql.Timestamp being a good example. It (indirectly) implements Comparable<java.util.Date>, not Comparable<Timestamp>.
 
She'll be back. I'm just gonna wait here. With this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic