- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我在结合泛型、实现
和内部类时遇到问题。我正在创建一个包含内部类的 LinkedBinaryHeap
类。这个内部类是泛型 HeapNode
,它扩展了我创建的泛型 Node
类;它只是为键/优先级添加一个变量和方法。
在 LinkedBinaryHeap
中,我创建了一个通用的 LinkedList
来存储 HeapNode
。我假设正在存储的通用数据扩展了 Comparable
类。
这是什么存储什么的布局:
BinaryHeap->LinkedList(Nodes)->HeapNode(extends Node)->DATA,KEY
我的问题是在声明 LinkedList
时:
LinkedList<HeapNode> heap;
eclipse 下划线 HeapNode
并给我错误:
Bound mismatch: The type LinkedBinaryHeap.HeapNode is not a valid substitute for the bounded parameter > of the type LinkedList
I think the error is telling me that HeapNode
must implement the Comparable
, however my Node
class implements Comparable
, so that is taken care of, correct?
I have tried all sorts of different things, but nothing seems to work, the below code is the closest I came. Note that I have tried leaving implements Comparable Node<T>
off the HeapNode
inner class, and it changes nothing.
Code:
LinkedBinaryHeap.java:
public class LinkedBinaryHeap<E extends Comparable<E>> {
private LinkedList<HeapNode> heap;
public LinkedBinaryHeap(){
heap = new LinkedList<HeapNode>();
}
/* INNER CLASS DECLARATION. */
private class HeapNode extends Node<E> implements Comparable<Node<E>>{
int key;
public HeapNode(int key, E data){
super(data);
this.key = key;
}
public int getKey(){
return key;
}
public void setKey(int key){
this.key = key;
}
}
}
节点.java:
public class Node<T extends Comparable<T>> implements Comparable<Node<T>>{
protected T data;
protected Node<T> next;
protected Node<T> previous;
public Node(T data){
next = null;
previous = null;
this.data = data;
}
/* Some other methods left out here. */
public int compareTo(Node<T> node) {
return data.compareTo(node.getData());
}
}
链表.java:
public class LinkedList<T extends Comparable<T>> implements Comparable<LinkedList<T>>{
private Node<T> head;
private Node<T> tail;
private int size;
public LinkedList(){
head = null;
tail = null;
size = 0;
}
/* Other methods left out. */
public int compareTo(LinkedList<T> list){
// does stuff.
}
}
最佳答案
根据您的定义:
HeapNode
是 Node<E>
的子类型但是implements Comparable<Node<E>>
LinkedList
需要一个类型参数使得 T implements Comparable<T>
LinkedList<HeapNode>
要求 HeapNode implements Comparable<HeapNode>
implements Comparable<Node<E>>
)所以两者不兼容。
你需要,在LinkedList
, 将节点类型表示为类型参数,适本地限制,以及节点类型的组件类型参数,也适本地限制:
public class LinkedList<N extends Node<E>,
E extends Comparable<E>>
implements Comparable<LinkedList<N, E>>{
private N head;
private N tail;
private int size;
...
现在您的 LinkedBinaryHeap
需要调整它对 LinkedList
的使用:
public class LinkedBinaryHeap<E extends Comparable<E>> {
private LinkedList<HeapNode, E> heap;
public LinkedBinaryHeap(){
heap = new LinkedList<HeapNode, E>();
}
现在应该可以编译了。它是否实现了将所有事物与其他事物进行比较的目标就很难说了!
关于Java:结合泛型、内部类和 "implements"的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28653444/
我经常在 C 标准文档中看到“实现定义”的说法,并且非常将其作为答案。 然后我在 C99 标准中搜索它,并且: ISO/IEC 9899/1999 (C99) 中第 §3.12 条规定: 3.12 I
“依赖于实现”中的“实现”是什么意思? “依赖于实现”和“依赖于机器”之间有什么区别? 我使用C,所以你可以用C解释它。 最佳答案 当 C 标准讨论实现时,它指的是 C 语言的实现。因此,C 的实现就
我刚刚在 Android-studio 中导入了我的项目,并试图在其中创建一个新的 Activity。但我无法在 android-studio 中创建 Activity 。我指的是here我看不到将目
我想知道您对为什么会发生此错误的意见。在陆上生产环境中,我们使用 CDH4。在我们的本地测试环境中,我们只使用 Apache Hadoop v2.2.0。当我运行在 CDH4 上编译的同一个 jar
我正在尝试集成第三方 SDK (DeepAR)。但是当我构建它时,它会显示一个错误。我试图修复它。如果我创建一个简单的新项目,它就可以正常工作。但是我现有的应用程序我使用相机和 ndk。请帮我找出错误
我很好奇为什么我们有 @Overrides 注释,但接口(interface)没有类似的习惯用法(例如 @Implements 或 @Implementation)。这似乎是一个有用的功能,因为您可能
我对 DAODatabase(适用于 Oracle 11 xe)的 CRUD 方法的实现感到困惑。问题是,在通常存储到 Map 集合的情况下,“U”方法(更新)会插入新元素或更新它(像 ID:Abst
Java-API 告诉我特定类实现了哪些接口(interface)。但有两种不同类型的信息,我不太确定这意味着什么。例如,对于“TreeSet”类:https://docs.oracle.com/en
我有一个接口(interface) MLService,它具有与机器学习算法的训练和交叉验证相关的基本方法,我必须添加两个接口(interface)分类和预测,它们将实现 MLService 并包含根
我一直想知道如何最好地为所有实现相同接口(interface)的类系列实现 equals()(并且客户端应该只使用所述接口(interface)并且永远不知道实现类)。 我还没有编写自己的具体示例,但
我有一个接口(interface)及其 2 个或更多实现, public interface IProcessor { default void method1() { //logic
我有同一个应用程序的免费版和高级版(几乎相同的代码,相同的类,到处都是“if”, list 中的不同包, list 中的进程名称相同)。主要 Activity 使用 IMPLICIT Intent 调
这是我为我的应用程序中的错误部分编写的代码 - (id)initWithData:(NSData *)data <-------- options:(NSUInteger)opti
请查找随附的代码片段。我正在使用此代码将文件从 hdfs 下载到我的本地文件系统 - Configuration conf = new Configuration(); FileSys
我想在 MongoDB 中使用 Grails2.5 中的“ElasticSearch”插件。我的“BuildConfig.groovy”文件是: grails.servlet.version = "3
我收到一条错误消息: fatal error: init(coder:) has not been implemented 对于我的自定义 UITableViewCell。该单元格未注册,在 Stor
得到这个错误 kotlin.NotImplementedError: An operation is not implemented: not implemented 我正在实现一个 ImageBut
typedef int Element; typedef struct { Element *stack; int max_size; int top; } Stack; //
Playground 代码 here 例子: interface IFoo { bar: number; foo?: () => void; } abstract class Abst
我想知道如何抑制警告: Category is implementing a method which will also be implemented by its primary class. 我
我是一名优秀的程序员,十分优秀!