- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在创建一个使用二叉搜索树实现通用 Set 的类。我在我的几个方法中使用了“compareTo”方法,无论我做什么,我都会不断收到规定的警告。感谢您的帮助!
// 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);
}
}
}
编辑:包括SimpleSet接口(interface):
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();
}
最佳答案
您方法上的签名使用原始 Comparable
没有泛型的接口(interface)。您的 set 实现中似乎要求数据类型实现 Comparable
, 所以既然你现在使用的是泛型,你应该全面地做出改变。
您没有发布 SimpleSet
的类声明, 所以 Comparable
对 E
的限制可能已经在那里了。如果不是,您需要将类声明更改为:
public class MySet<E extends Comparable<? super E>> implements SimpleSet<E>
这会告诉您的类的客户只有实现 Comparable
的类型允许作为集合实现的泛型类型参数。您没有发布 BTNode
的代码, 但它可能需要在 E
上进行参数化以及 ( BTNode<E>
)。
现在,因为您只允许 E
类型的对象要添加到集合中,您应该更改加法器方法以反射(reflect)这一点:
private BTNode<E> addToSubtree(BTNode<E> r, E elem) throws DuplicateAdded
以此类推,对于subtreeContains
, 等等。 Java泛型的要点是,你用类型占位符( Comparable
代表元素)替换所有这些地方(到 E
,在你的情况下),在编译时限制可以添加的内容,所以废除最需要显式转换。泛型是一个强大但复杂的特性,我推荐阅读 the official tutorial .
关于java - 警告 : unchecked call to compareTo(T) as a member of the raw type java. lang.Comparable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18143305/
我正尝试在一些 native Rust-C 绑定(bind)上构建一个安全的包装器。我正在引用 git2-rs代码库,我遇到了以下用法: use raw; use util::Binding; pub
我想使用用户提供的字符串作为 JavaScript 函数的参数,因此需要转义所有可能导致脚本中断的字符。 这是为了与处理原始 JavaScript 的 WKWebView.evaluateJavaSc
我需要用 C# 解析一个在 Wireshark 中生成的 pcap 文件。当然,它可以使用 wireshark 正常打开并按预期显示所有数据包。 我曾尝试使用我在网上找到的两个流行的库(来自 Shar
knex.raw(sql, bindings)和 knex.schema.raw(statement) . 似乎这两个函数具有不同的签名。 如果它们相等,我该如何使用 knex.schema.raw(
我是这里的新手。 我想知道任何工具/快速方法来转换具有 3 字节 PCM 样本的 24 位 PCM 原始( headless )文件, 成一个 32 位 PCM 原始文件,每个样本有 4 个字节,4
Unhandled rejection Error: where: "raw query" has been removed, please use where ["raw query", [repl
我的任务是打开一个扩展名为 mka 的现有音频文件(Matroska 容器)并提取原始音频数据。 This示例仅显示了从 mp2 文件中提取原始数据的示例。我不知道如何使用 mka 容器执行此操作。我
在 Zend Framework 的 Response Class 中,有两个不同的数组用于存储 header :_headers[] 和 _headersRaw[]。并且有适当的方法来设置每一个:
我们可以直接从Github链接文件吗?。我知道这在谷歌代码上是允许的。这样,我就不必担心更新本地文件了。
在 TCP 中,我从 IP 摄像机接收媒体流作为 RAW。根据那里的建议,我需要把它写成文件。然后我可以用 VLC 等媒体播放器播放它。 但是当我将其写入文件并使用媒体播放器播放时,它永远不会播放损坏
我对码头公司还是个新手。我使用的是最新版本的Python、Django和Docker。我已经在这个项目上工作了两周了,我已经创建了docker-compose.yml文件,并且已经构建了我的docke
我有两只鼠标连接到我的计算机,我想制作一个记录器来区分这两者。低级鼠标 Hook 不向我提供该信息,因此我考虑捕获原始输入消息以获取鼠标的设备实例 ID。但不幸的是,原始输入寄存器仅限于我的应用程序!
我对 Laravel 还很陌生,到目前为止,我真的很喜欢 eloquent 和 querybuilder,但是一旦查询变得更加复杂,我的头就开始受伤......我刚刚完成了相当长一段时间后的 2 个工
我很困惑,真的不知道该如何选择在何处使用这两者? 我都阅读了文档 https://laravel.com/docs/5.4/queries#where-clauses 和 https://larave
mysql 表 -> 表名称td id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT, band varchar(4) NOT NULL, PRIMARY KE
我在 ASP.net MVC 中呈现 HTML 页面: @(Html.Raw(@Model.Body)) 用于包含在模型的 Body 属性中的格式化和样式文本,但这会将 CSS 样式更改为整个页面。我
使用以下 python 读取和显示(灰度)RAW 图像: import numpy as np import matplotlib.pyplot as plt path = 'path\\to\\wh
我正在我的应用程序中构建一个 MP3 播放器,但我收到一条错误消息,指出“raw cannot be resolved or is not a field”在线:mMediaPlayer = Medi
我正在尝试使用枚举作为 Hibernate 中 map 的映射键,但 Hibernate 将我的枚举存储为 RAW: 我有这个枚举: public enum AccountType implement
我想在 python 中构建一个数据包嗅探器,它能够嗅探数据包、分析它们并在第二步中将数据包注入(inject)本地接口(interface)。 我找到了一个示例,我必须稍微调整一下才能工作。我的工作
我是一名优秀的程序员,十分优秀!