求大神:下面是我的Java代码,怎么避免里面的@SuppressWarnings("unchecked")?谢谢
packagetree;publicclassBinarySearchTree<T>{privateComparable<T>obj;privateBinarySearc...
package tree;
public class BinarySearchTree<T> {
private Comparable<T> obj;
private BinarySearchTree<T> leftTree;
private BinarySearchTree<T> rightTree;
public BinarySearchTree(Comparable<T> obj){
this.obj = obj;
this.leftTree = null;
this.rightTree = null;
}
@SuppressWarnings("unchecked")
public void insert(Comparable<T> rhs) {
if(this.obj.compareTo((T) rhs) < 0) {
if(this.leftTree == null) {
this.leftTree = new BinarySearchTree<T>(rhs);
} else {
this.leftTree.insert(rhs);
}
} else if(this.obj.compareTo((T) rhs) > 0) {
if(this.rightTree == null) {
this.rightTree = new BinarySearchTree<T>(rhs);
} else {
this.rightTree.insert(rhs);
}
}
}
public void printTree() {
System.out.print(this.obj);
System.out.print(" ");
if(this.leftTree != null)
this.leftTree.printTree();
if(this.rightTree != null)
this.rightTree.printTree();
}
public static void main(String[] args) {
BinarySearchTree<Integer> bst = new BinarySearchTree<Integer>(8);
bst.printTree();
bst.insert(6);
bst.insert(9);
bst.insert(5);
bst.insert(7);
bst.insert(1);
bst.printTree();
}
} 展开
public class BinarySearchTree<T> {
private Comparable<T> obj;
private BinarySearchTree<T> leftTree;
private BinarySearchTree<T> rightTree;
public BinarySearchTree(Comparable<T> obj){
this.obj = obj;
this.leftTree = null;
this.rightTree = null;
}
@SuppressWarnings("unchecked")
public void insert(Comparable<T> rhs) {
if(this.obj.compareTo((T) rhs) < 0) {
if(this.leftTree == null) {
this.leftTree = new BinarySearchTree<T>(rhs);
} else {
this.leftTree.insert(rhs);
}
} else if(this.obj.compareTo((T) rhs) > 0) {
if(this.rightTree == null) {
this.rightTree = new BinarySearchTree<T>(rhs);
} else {
this.rightTree.insert(rhs);
}
}
}
public void printTree() {
System.out.print(this.obj);
System.out.print(" ");
if(this.leftTree != null)
this.leftTree.printTree();
if(this.rightTree != null)
this.rightTree.printTree();
}
public static void main(String[] args) {
BinarySearchTree<Integer> bst = new BinarySearchTree<Integer>(8);
bst.printTree();
bst.insert(6);
bst.insert(9);
bst.insert(5);
bst.insert(7);
bst.insert(1);
bst.printTree();
}
} 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询