【发布时间】:2012-07-01 22:44:27
【问题描述】:
首先我搜索了 java 中泛型类型的用法,但是我发现的答案太简单或太复杂了。所以这是我的确切问题。
我有三个类,分别是 PerfectTreeControl、Tree 和 Entry。
树有
public class Tree<K> { public Entry <K> root;
条目有
public class Entry<K> {
public K element;
public Entry<K> parent, left_child, right_child;
public Entry(K element) {
this.element = element;
}
public Entry(K element, Entry<K> left, Entry<K> right) {
left_child = left;
right_child = right;
this.element = element;
}
我想了解 Entry 父级和 Entry K element 可以用作整数、字符串或我想要的任何东西,但同样的事情也适用于对象吗?我尝试使用不带参数的 Entry 变量,它只说 Entry 是原始类型,应该参数化,它仍然可以正常工作。
我的第二个问题是关于检查一棵树是否完美。以下是我迄今为止尝试过的一些代码:
public class PerfectTreeControl {
public static boolean isPerfect(Tree<String> tree) {
Tree t1 = new Tree();
if( t1.isFull( tree.root ) ) {
int depth = t1.height(tree.root);
return t1.everyLeafHasSameDepth(tree.root, depth);
}
else
return false;
}
}
public class Tree<K> {
public Entry <K> root;
public boolean isLeaf(Entry e) {
return e.left_child == null &&
e.right_child == null;
}
public int height(Entry e) {
if( e == null ||
e.left_child == null &&
e.right_child == null )
return 0;
int left = height( e.left_child );
int right = height( e.right_child );
return 1 + Math.max(left, right);
}
public boolean isFull(Entry base) {
if( isLeaf(base) )
return true;
else
if( base.left_child != null && base.right_child != null ) {
return isFull(base.left_child) &&
isFull(base.right_child);
} else {
return false;
}
}
public int depth(Entry e) {
if( e == root ) {
return 0;
} else {
return 1 + depth(e.parent);
}
}
public boolean everyLeafHasSameDepth(Entry base, int depth) {
if( base == null )
return false;
else if(isLeaf(base) )
return depth( base ) == depth;
else {
return
everyLeafHasSameDepth(base.left_child, depth) &&
everyLeafHasSameDepth(base.right_child, depth);
}
}
- 入口类(我写在页面顶部) 正如你所见,PerfectTreeControl类中的isPerfect方法使用Tree-String-tree作为参数,我不知道它是什么。在 Tree 类中,我尝试了 Entry 并且一次又一次没有区别。代码无法正常工作,我完全糊涂了。
【问题讨论】:
-
您不应该真正担心原始类型。它们只是为了向后兼容而存在的,您永远不应该使用泛型类型的原始版本。 (即在您的代码中,到处使用
Entry<Something>。) -
定义泛型对象的意义何在?更清楚地说,将对象定义为泛型而不是普通对象有什么好处?对象已经可以将您在类中定义的任何内容作为变量、字符串、int 等。我错过了什么吗?
-
@nihirus:如果您在任何地方都使用普通对象,那么每次您想做某事时都需要将值转换为所需的类型。
-
@nihirus 主要是类型安全。我强烈建议至少阅读臭名昭著的 Angelika Langer FAQ 的开头部分
标签: java generics binary-tree implementation