为什么会有WARNNING??/* BST.java
*/
public class BST
{
private static class Node
{
Object data;
Node left = null;
Node right = null;
Node(Object data)
{
this.data = data;
}
}
private Node root = null;
private int size = 0;
public BST()
{
} public int size()
{
return size;
} private String insert(Node parent, Object obj)
{
int cmp = ((Comparable)obj).compareTo((Comparable)parent.data);
if (cmp == 0)
return "failure - duplicate entry";
if (cmp < 0)
{
if (parent.left == null)
{
parent.left = new Node(obj);
size++;
return "success";
}
else
return insert(parent.left, obj);
}
// cmp > 0
if (parent.right == null)
{
parent.right = new Node(obj);
size++;
return "success";
}
return insert(parent.right, obj);
} public String insert(Object obj)
{
if (root == null)
{
root = new Node(obj);
size++;
return "success";
}
return insert(root, obj);
} private boolean contains(Node parent, Object obj)
{
if (parent == null)
return false;
int cmp = ((Comparable)obj).compareTo((Comparable)parent.data);
if (cmp == 0)
return true;
if (cmp < 0)
return contains(parent.left, obj);
// cmp > 0
return contains(parent.right, obj);
}
public boolean contains(Object obj)
{
return contains(root, obj);
}
// *******************************************************
public static void main(String[] s)
{
BST bst = new BST();
bst.insert(new String("may"));
bst.insert(new String("fred"));
bst.insert(new String("sue"));
bst.insert(new String("daxa"));
bst.insert(new String("eva"));
bst.insert(new String("tom"));
bst.insert(new String("nita"));
bst.insert(new String("tom")); // duplicate entry
System.out.println("The size of the tree is ... " +
bst.size());
System.out.println("tree contains ann ... " +
bst.contains(new String("ann")));
System.out.println("tree contains may ... " +
bst.contains(new String("may")));
System.out.println("tree contains tom ... " +
bst.contains(new String("tom")));
System.out.println("tree contains ursula ... " +
bst.contains(new String("ursula")));
}
}
*/
public class BST
{
private static class Node
{
Object data;
Node left = null;
Node right = null;
Node(Object data)
{
this.data = data;
}
}
private Node root = null;
private int size = 0;
public BST()
{
} public int size()
{
return size;
} private String insert(Node parent, Object obj)
{
int cmp = ((Comparable)obj).compareTo((Comparable)parent.data);
if (cmp == 0)
return "failure - duplicate entry";
if (cmp < 0)
{
if (parent.left == null)
{
parent.left = new Node(obj);
size++;
return "success";
}
else
return insert(parent.left, obj);
}
// cmp > 0
if (parent.right == null)
{
parent.right = new Node(obj);
size++;
return "success";
}
return insert(parent.right, obj);
} public String insert(Object obj)
{
if (root == null)
{
root = new Node(obj);
size++;
return "success";
}
return insert(root, obj);
} private boolean contains(Node parent, Object obj)
{
if (parent == null)
return false;
int cmp = ((Comparable)obj).compareTo((Comparable)parent.data);
if (cmp == 0)
return true;
if (cmp < 0)
return contains(parent.left, obj);
// cmp > 0
return contains(parent.right, obj);
}
public boolean contains(Object obj)
{
return contains(root, obj);
}
// *******************************************************
public static void main(String[] s)
{
BST bst = new BST();
bst.insert(new String("may"));
bst.insert(new String("fred"));
bst.insert(new String("sue"));
bst.insert(new String("daxa"));
bst.insert(new String("eva"));
bst.insert(new String("tom"));
bst.insert(new String("nita"));
bst.insert(new String("tom")); // duplicate entry
System.out.println("The size of the tree is ... " +
bst.size());
System.out.println("tree contains ann ... " +
bst.contains(new String("ann")));
System.out.println("tree contains may ... " +
bst.contains(new String("may")));
System.out.println("tree contains tom ... " +
bst.contains(new String("tom")));
System.out.println("tree contains ursula ... " +
bst.contains(new String("ursula")));
}
}
The size of the tree is ... 7
tree contains ann ... false
tree contains may ... true
tree contains tom ... true
tree contains ursula ... false没有异常不知道 lz的 报什么 警告 ,难道是你的 jdk 版本 低 引起的
定义接口继承Comparable
@SuppressWarnings({"unchecked"})
来取消警告