0

I have a class Zeitpunkt which implements a date with time and in addition a class Suchbaum which represents a binary search tree.

I want to use a Comparator-Object in Suchbaum to sort a tree by the day of Zeitpunkt, but when I want to create a Suchbaum object, it prints the named error.

Zeipunkt

public class Zeitpunkt<T> implements Comparable<T>
{
private int jahr;
private int monat;
private int tag;
private int stunden;
private int minuten;
private double sekunden;

public int vergleich(Zeitpunkt a) { ... }

@Override
public int compareTo(T o) {
    if(o instanceof Zeitpunkt)
        return vergleich((Zeitpunkt)o);
    return 0;
}
...
}

Suchbaum

public class Suchbaum<T extends Comparable<T>> {
private class Element {
    private T daten;
    private Element links;
    private Element rechts;

    public Element(T t) {
        daten = t;
        links = null;
        rechts = null;
    }
}
private Element wurzel;
private Comparator<T> comp;
...
}

Testclass

public class BaumTest {
public static void main(String[] args) {

// error in the following line (IntelliJ underlines the first 
// "Zeitpunkt"). Suchbaum<Zeitpunkt<?>> = ... doesn't work either..
// *Completely confused*
Suchbaum<Zeitpunkt> sb = new Suchbaum<>((Zeitpunkt z1, Zeitpunkt z2) -> {
        if(z1.getTag() > z2.getTag())
            return 1;
        else if(z1.getTag() == z2.getTag())
            return 0;
        else
            return -1;
    });
}
}

Any ideas? (the other threads with this topic didn't help me out)

1
  • 4
    Try and use Englih names in code; this is a generally good habit as it allows more people to help and/or understand your code. Commented May 9, 2015 at 12:14

1 Answer 1

3

Seems that you don't want to make your Zeitpunkt class parametrized, you just want it to implement Comparable interface. So change it like this:

public class Zeitpunkt implements Comparable<Zeitpunkt> {
    private int jahr;
    private int monat;
    private int tag;
    private int stunden;
    private int minuten;
    private double sekunden;

    public int vergleich(Zeitpunkt a) {
        return 0;
    }

    @Override
    public int compareTo(Zeitpunkt o) {
        return vergleich(o);
    }
}

Also you need to define a constructor in your Suchbaum class:

public Suchbaum(Comparator<T> comp) {
    this.comp = comp;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.