比较 Java 中可比较的泛型
Comparing Generics that are comparable in Java
我有一个通用二叉树,它会将小于或等于的对象添加到左侧,将大于的对象添加到右侧。我的问题是比较泛型,我知道数据值将是一个对象包装的原语或一个字符串,所以它们是可比较的。但是,我不知道如何在代码中实现它。
代码还在开发中,我知道添加方法还不能正确添加,但我正在努力。谢谢
这是树节点:
public class TreeNode<T>
{
//Instance Variables
TreeNode leftChild;
TreeNode rightChild;
int childCount;
int depth;
T data;
public TreeNode(T data, int parentDepth)
{
leftChild = null;
rightChild = null;
childCount = 0;
depth = parentDepth + 1;
this.data = data;
}
public TreeNode(int parentDepth)
{
leftChild = null;
rightChild = null;
childCount = 0;
depth = parentDepth + 1;
data = null;
}
public void add(T data)
{
if (this.data.compareTo(data) <= 0)
{
addLeft(data);
} else if (this.data.compareTo(data) > 0)
{
addRight(data);
} else
{
System.out.println("ERROR IN TREENODE.ADD");
}
}
public void addLeft(T data)
{
leftChild = new TreeNode(data, depth);
}
public void addLeft()
{
leftChild = new TreeNode(depth);
}
public void addRight(T data)
{
rightChild = new TreeNode(data, depth);
}
public void addRight() {
rightChild = new TreeNode(depth);
}
}
I know the data value will be a object-wrapped primitive or a String, so they are comparable.
然后你可以告诉编译器:
public class TreeNode<T extends Comparable<T>>
如果这样做,您将可以访问 Comparable
中定义的 compareTo
方法。
您的 T 应该实现 Comparable 接口以便进行比较。
public class TreeNode<T extends Comparable<T>>
{
//Instance Variables
TreeNode leftChild;
TreeNode rightChild;
int childCount;
int depth;
T data;
public TreeNode(T data, int parentDepth)
{
leftChild = null;
rightChild = null;
childCount = 0;
depth = parentDepth + 1;
this.data = data;
}
public TreeNode(int parentDepth)
{
leftChild = null;
rightChild = null;
childCount = 0;
depth = parentDepth + 1;
data = null;
}
public void add(T data)
{
if (this.data.compareTo(data) <= 0)
{
addLeft(data);
} else if (this.data.compareTo(data) > 0)
{
addRight(data);
} else
{
System.out.println("ERROR IN TREENODE.ADD");
}
}
public void addLeft(T data)
{
leftChild = new TreeNode(data, depth);
}
public void addLeft()
{
leftChild = new TreeNode(depth);
}
public void addRight(T data)
{
rightChild = new TreeNode(data, depth);
}
public void addRight() {
rightChild = new TreeNode(depth);
}
}
我有一个通用二叉树,它会将小于或等于的对象添加到左侧,将大于的对象添加到右侧。我的问题是比较泛型,我知道数据值将是一个对象包装的原语或一个字符串,所以它们是可比较的。但是,我不知道如何在代码中实现它。
代码还在开发中,我知道添加方法还不能正确添加,但我正在努力。谢谢
这是树节点:
public class TreeNode<T>
{
//Instance Variables
TreeNode leftChild;
TreeNode rightChild;
int childCount;
int depth;
T data;
public TreeNode(T data, int parentDepth)
{
leftChild = null;
rightChild = null;
childCount = 0;
depth = parentDepth + 1;
this.data = data;
}
public TreeNode(int parentDepth)
{
leftChild = null;
rightChild = null;
childCount = 0;
depth = parentDepth + 1;
data = null;
}
public void add(T data)
{
if (this.data.compareTo(data) <= 0)
{
addLeft(data);
} else if (this.data.compareTo(data) > 0)
{
addRight(data);
} else
{
System.out.println("ERROR IN TREENODE.ADD");
}
}
public void addLeft(T data)
{
leftChild = new TreeNode(data, depth);
}
public void addLeft()
{
leftChild = new TreeNode(depth);
}
public void addRight(T data)
{
rightChild = new TreeNode(data, depth);
}
public void addRight() {
rightChild = new TreeNode(depth);
}
}
I know the data value will be a object-wrapped primitive or a String, so they are comparable.
然后你可以告诉编译器:
public class TreeNode<T extends Comparable<T>>
如果这样做,您将可以访问 Comparable
中定义的 compareTo
方法。
您的 T 应该实现 Comparable 接口以便进行比较。
public class TreeNode<T extends Comparable<T>>
{
//Instance Variables
TreeNode leftChild;
TreeNode rightChild;
int childCount;
int depth;
T data;
public TreeNode(T data, int parentDepth)
{
leftChild = null;
rightChild = null;
childCount = 0;
depth = parentDepth + 1;
this.data = data;
}
public TreeNode(int parentDepth)
{
leftChild = null;
rightChild = null;
childCount = 0;
depth = parentDepth + 1;
data = null;
}
public void add(T data)
{
if (this.data.compareTo(data) <= 0)
{
addLeft(data);
} else if (this.data.compareTo(data) > 0)
{
addRight(data);
} else
{
System.out.println("ERROR IN TREENODE.ADD");
}
}
public void addLeft(T data)
{
leftChild = new TreeNode(data, depth);
}
public void addLeft()
{
leftChild = new TreeNode(depth);
}
public void addRight(T data)
{
rightChild = new TreeNode(data, depth);
}
public void addRight() {
rightChild = new TreeNode(depth);
}
}