警告:未选中调用compareTo(T)作为原始类型java.lang.Comparable的成员
我正在创建一个类,该类使用二进制搜索树实现一个泛型集。我在我的几个方法中使用了“compareTo”方法,无论我做什么,我都会不断得到声明的警告。感谢您的帮助警告:未选中调用compareTo(T)作为原始类型java.lang.Comparable的成员,java,generics,Java,Generics,我正在创建一个类,该类使用二进制搜索树实现一个泛型集。我在我的几个方法中使用了“compareTo”方法,无论我做什么,我都会不断得到声明的警告。感谢您的帮助 // Allow short name access to following classes import csc143.data_structures.*; public class MySet<E> implements SimpleSet<E> { // the root of the "tree"
// Allow short name access to following classes
import csc143.data_structures.*;
public class MySet<E> implements SimpleSet<E> {
// the root of the "tree" that structures the set
private BTNode root;
// the current number of elements in the set
private int numElems;
public MySet() {
root = null;
numElems = 0;
}
/**
* Add an element to the set.
*
* @param e The element to be added to the set.
* @return <tt>true</tt> If this operation updated the contents of the set.
*/
public boolean add(E e) {
try {
root = addToSubtree(root, (Comparable) e);
return true;
} catch(DuplicateAdded exc) {
// duplicate trying to be added
return false;
}
}
// This helper method adds the element "e" to tree rooted at r. Returns
// (possibly new) tree containing "e", or throws DuplicateAdded exception
// if "e" already exists in tree.
private BTNode addToSubtree(BTNode r, Comparable elem)
throws DuplicateAdded {
if(r == null) {
return new BTNode(elem);
}
int compare = elem.compareTo(r.item);
// element already in tree
if(compare == 0) {
throw new DuplicateAdded("Element is already in set");
}
if(compare < 0) {
r.left = addToSubtree(r.left, elem);
} else { // compare > 0
r.right = addToSubtree(r.right, elem);
}
// element has been added
return r;
}
/**
* Remove all elements from this set.
*/
public void clear() {
root = null;
numElems = 0;
}
/**
* Checks for the existance of the specified value within the set.
*
* @param e The value sought.
* @return <tt>true</tt> If the value exists in the set.
*/
public boolean contains(E e) {
return subtreeContains(root, (Comparable) e);
}
// This helper method returns whether element "elem" is in
// (sub-)tree with root "r".
private boolean subtreeContains(BTNode r, Comparable elem) {
if(r == null) {
return false;
} else {
int compare = elem.compareTo(r.item);
// found element
if(compare == 0){
return true;
} else if(compare < 0) {
return subtreeContains(r.left, elem);
} else { // compare > 0
return subtreeContains(r.right, elem);
}
}
}
/**
* Check for the existance of elements in the set.
*
* @return <tt>true</tt> If there are no elements in the set.
*/
public boolean isEmpty() {
return root == null;
}
/**
* Return the number of elements in the set.
*
* @return The number of elements in the set.
*/
public int size() {
return numElems;
}
/**
* Returns a String representation of the contents of the set.
*
* @return The String representation of the set.
*/
public String toString() {
}
// this inner class creates the node that compose the binary tree structure
class BTNode<E> {
/**
* The item stored in the node.
*/
public E item;
/**
* The node to the left of "this" node.
*/
public BTNode left;
/**
* The node to the right of "this" node.
*/
public BTNode right;
/**
* Constructs the BTNode object (three parameters).
*
* @param item The item to be stored in the node.
* @param left The node to the left of "this" node.
* @param right The node to the right of "this" node.
*/
@SuppressWarnings("unchecked")
public BTNode(Object item, BTNode left, BTNode right) {
// bind to references
this.item = (E) item;
this.left = left;
this.right = right;
}
/**
* Constructs the BTNode (one parameter).
*
* @param The item to be stored in the node.
*/
public BTNode(Object item) {
// call three parameter constructor
this(item, null, null);
}
}
}
//允许短名称访问以下类
导入csc143.数据_结构。*;
公共类MySet实现SimpleSet{
//构成集合的“树”的根
私有节点根;
//集合中当前的元素数
私人国际婚礼;
公共MySet(){
root=null;
numlems=0;
}
/**
*向集合中添加元素。
*
*@param e要添加到集合中的元素。
*@如果此操作更新了集合的内容,则返回true。
*/
公共布尔加法(E){
试一试{
根=添加到子树(根,(可比)e);
返回true;
}捕获(重复添加的exc){
//试图添加重复项
返回false;
}
}
//这个helper方法将元素“e”添加到以r为根的树中
//(可能是新的)包含“e”的树,或抛出重复的异常
//如果树中已经存在“e”。
专用BTNode addToSubtree(BTNode r,可比元素)
添加了重复项{
if(r==null){
返回新的BTNode(elem);
}
int比较=元素比较(r项);
//元素已在树中
如果(比较==0){
抛出新的重复添加(“元素已在集合中”);
}
如果(比较<0){
r、 左=添加到子树(右左,元素);
}否则{//compare>0
r、 右=添加到子树(右,元素);
}
//元素已添加
返回r;
}
/**
*从该集中删除所有元素。
*/
公共空间清除(){
root=null;
numlems=0;
}
/**
*检查集合中是否存在指定值。
*
*@param e所寻求的值。
*@如果集合中存在该值,则返回true。
*/
公共布尔包含(E){
返回子树包含(根,(可比)e);
}
//这个helper方法返回元素“elem”是否在
//根为“r”的(子)树。
私有布尔子树包含(BTNode r,可比较元素){
if(r==null){
返回false;
}否则{
int比较=元素比较(r项);
//发现元素
如果(比较==0){
返回true;
}否则如果(比较<0){
返回子目录包含(左、右、元素);
}否则{//compare>0
返回子目录包含(右,元素);
}
}
}
/**
*检查集合中是否存在元素。
*
*@如果集合中没有元素,则返回true。
*/
公共布尔值为空(){
返回root==null;
}
/**
*返回集合中的元素数。
*
*@返回集合中的元素数。
*/
公共整数大小(){
回礼;
}
/**
*返回集合内容的字符串表示形式。
*
*@返回集合的字符串表示形式。
*/
公共字符串toString(){
}
//这个内部类创建组成二叉树结构的节点
类BTNode{
/**
*存储在节点中的项。
*/
公共电子项目;
/**
*“此”节点左侧的节点。
*/
公共BTNode左;
/**
*“此”节点右侧的节点。
*/
公共节点权;
/**
*构造BTNode对象(三个参数)。
*
*@param item要存储在节点中的项。
*@param将节点放在“this”节点的左侧。
*@param right将节点置于“this”节点的右侧。
*/
@抑制警告(“未选中”)
公共BTNode(对象项,BTNode左,BTNode右){
//绑定到引用
本项=(E)项;
this.left=左;
这个。右=右;
}
/**
*构造BTNode(一个参数)。
*
*@param要存储在节点中的项。
*/
公共BTNode(对象项){
//调用三参数构造函数
此(项,空,空);
}
}
}
编辑:包括SimpleSet界面:
package csc143.data_structures;
public interface SimpleSet<E> {
/**
* Add an element to the set.
*
* @param e The element to be added to the set.
* @return <tt>true</tt> If this operation updated the contents of the set.
*/
public boolean add(E e);
/**
* Remove all elements from this set.
*/
public void clear();
/**
* Checks for the existance of the specified value within the set.
*
* @param e The value sought.
* @return <tt>true</tt> If the value exists in the set.
*/
public boolean contains(E e);
/**
* Check for the existance of elements in the set.
*
* @return <tt>true</tt> If there are no elements in the set.
*/
public boolean isEmpty();
/**
* Return the number of elements in the set.
*
* @return The number of elements in the set.
*/
public int size();
/**
* Returns a String representation of the contents of the set.
*
* @return The String representation of the set.
*/
public String toString();
}
包csc143.data\u结构;
公共接口SimpleSet{
/**
*向集合中添加元素。
*
*@param e要添加到集合中的元素。
*@如果此操作更新了集合的内容,则返回true。
*/
公共布尔加法(E);
/**
*从该集中删除所有元素。
*/
公共空间清除();
/**
*检查集合中是否存在指定值。
*
*@param e所寻求的值。
*@如果集合中存在该值,则返回true。
*/
公共布尔包含(E);
/**
*检查集合中是否存在元素。
*
*@如果集合中没有元素,则返回true。
*/
公共布尔值为空();
/**
*返回集合中的元素数。
*
*@返回集合中的元素数。
*/
公共整数大小();
/**
*返回集合内容的字符串表示形式。
*
*@返回集合的字符串表示形式。
*/
公共字符串toString();
}
方法上的签名使用原始可比较的
接口,而不使用泛型。在set实现中似乎有一个要求,即数据类型实现compariable
,因此,既然您现在使用泛型,就应该全面地进行更改
您没有发布SimpleSet
的类声明,因此Comparable
对E
的限制可能已经存在。如果不是,则需要将类声明更改为:
public class MySet<E extends Comparable<? super E>> implements SimpleSet<E>
等等,对于子目录contains
,等等。Java泛型的要点是用类型占位符(E
for element)替换所有的强制转换(在您的例子中,是到Comparable
),这些占位符在编译时限制了可以添加的内容,从而消除了对
private BTNode<E> addToSubtree(BTNode<E> r, E elem) throws DuplicateAdded
if(elem instanceof BTNode){
elem.compareTo(r.item);
}