I have a class Zeitpunkt which implements a date with time and in addition a class Suchbaum which represents a binary search tree.
我有一个类Zeitpunkt,它实现了一个带有时间的日期,另外还有一个类Suchbaum,表示一个二叉搜索树。
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.
我想使用Suchbaum中的Comparator-Object来按Zeitpunkt的日期对树进行排序,但是当我想创建一个Suchbaum对象时,它会打印命名错误。
Zeipunkt
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
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
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 个解决方案
#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:
似乎您不想让Zeitpunkt类参数化,而只想让它实现可比接口。所以像这样改变它:
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:
还需要在Suchbaum类中定义构造函数:
public Suchbaum(Comparator<T> comp) {
this.comp = comp;
}
#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:
似乎您不想让Zeitpunkt类参数化,而只想让它实现可比接口。所以像这样改变它:
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:
还需要在Suchbaum类中定义构造函数:
public Suchbaum(Comparator<T> comp) {
this.comp = comp;
}