Java比较泛型类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20793082/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java comparing generic types
提问by Alex_B
In Java, I wrote a Binary Search Tree class that adds nodes using recursion. Now I want to generalize it using Generics so I can learn more about them.
在 Java 中,我编写了一个使用递归添加节点的二叉搜索树类。现在我想使用泛型对其进行概括,以便我可以了解更多关于它们的信息。
public class GBinNode<T> {
T item;
GBinNode<T> left;
GBinNode<T> right;
public GBinNode(T newItem) {
item = newItem;
left = null;
right = null;
}
public GBinNode(T it, GBinNode<T> le, GBinNode<T> ri) {
item = it;
left = le;
right = ri;
}
public String toString() {
return item.toString()+" ";
}
}
My function to add nodes is in the following class
我添加节点的功能在以下类中
public class GBinTree<T extends Comparable <T>> {
GBinNode<T> add(T item, GBinNode<T> bn) {
if (bn==null) {
return new GBinNode<T>(item, null, null);
}
if (item < bn.item) { // ERROR HERE
bn.left = add( item, bn.left);
}
else {
bn.right = add( item, bn.right);
}
return bn;
}
public void toString(GBinNode<T> root) {
GBinNode<T> curr = root;
if (curr == null)
return;
else {
toString(curr.left);
System.out.println(curr.toString()); // inorder traversal
toString(curr.right);
}
}
The main class has the following code to kick things off. I'm using strings, but the data type could be some complex type.
主类具有以下代码来启动。我正在使用字符串,但数据类型可能是某种复杂类型。
GBinTree<String> bt = new GBinTree<String>();
GBinNode<String> root = null;
root = bt.add("Calex", root);
root = bt.add("Ealex", root);
root = bt.add("Balex", root);
root = bt.add("Dalex", root);
bt.toString(root);
I started to use the Comparable interface but then how do I write the CompareTo() function? I don't know what type T will be? The error I got was "The operator < is undefined for the argument type(s) T, T".
我开始使用 Comparable 接口,但是如何编写 CompareTo() 函数?不知道T会是什么类型?我得到的错误是“运算符 < 未定义参数类型 T、T”。
Searching for a solution, one answer was Comparing generic types Java:
寻找解决方案,一个答案是比较泛型 Java:
class Element<T extends Comparable<T>>
I don't understand where this should go, and how it's different from the class implementing Comparable. The only place I know the type is in the main class, so should the compareTo() be there? I looked at making GBinTree an interface, but got confused whether that was the right track? Any help would be appreciated.
我不明白这应该去哪里,以及它与实现 Comparable 的类有何不同。我知道类型的唯一地方是在主类中,那么 compareTo() 应该在那里吗?我看着让 GBinTree 成为一个界面,但很困惑这是否是正确的轨道?任何帮助,将不胜感激。
采纳答案by Sotirios Delimanolis
You cannot overload operators in Java. The <
operator only applies to primitive (or numeric) types, not reference types. Since T
is a type variable that represents a reference type, you cannot use <
on variables of type T
. You have to use
您不能在 Java 中重载运算符。该<
运算符仅适用于原始(或数字)类型,而不适用于引用类型。由于T
是表示引用类型的类型变量,因此不能<
在类型为 的变量上使用T
。你必须使用
if (item.compareTo(bn.item) < 0)
check the value returned and decide to do what you wish with it.
检查返回的值并决定用它做你想做的事。
You don't know what the type T
will be but you know that it will be a type that implements Comparable
and therefore implements the compareTo()
method.
您不知道该类型T
是什么,但您知道它将是一种实现Comparable
并因此实现该compareTo()
方法的类型。
回答by user2483498
You can use this simple approach
for data greater than root.getData = 1, for data equals root.getData = 0,for data lesser than root.getData = -1
对于大于 root.getData = 1 的数据,对于等于 root.getData = 0 的数据,对于小于 root.getData = -1 的数据,您可以使用这种简单的方法
public class BST<E extends Number & Comparable<? super E>>{
void add(){
...
if(data.compareTo(root.getData()) == 1)
...
}