- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我试图理解为什么会出现此错误:
-- 这是错误--
File: ...\HashTable.java [line: 103]
Error: The return type is incompatible with HashTableInterface<Key,Value>.iterators()
该接口(interface)包含一个内部类:
public class Entry<Key, Value>{...}
LinkedList 与 java API 类似,但它包含迭代器内部类,但在测试更简单的东西时,例如:
-- 这是工作代码,至少在 drjava 的交互 Pane 中是这样 --
LinkedList<String> list = new LinkedList<String>();
Iterator<String> test = list.iterator();
但是我无法在我的 HashTable 类中编译此方法:
-- 这是我无法运行的代码 --
public Iterator<Entry<Key, Value>> iterator(){
LinkedList<Entry<Key, Value>> iter = new LinkedList<Entry<Key, Value>>();
return iter.iterator();
}
我认为它很简单,看起来并不太复杂,但任何输入将不胜感激。
** 你很抱歉它应该是 list.iterator();
编辑
-- 好的,这是界面 --
import nhUtilities.containers2.*;
interface HashTableInterface<Key, Value> {
public boolean isEmpty();
public Value get(Key key);
public void add(Key key, Value value);
public void remove(Key key);
public void clear();
public Iterator<Entry<Key, Value>> iterator();
public interface Entry<Key, Value> {
public Key getKey();
public Value getValue();
public void setValue(Value value);
}
}
--这是已实现的条目,它是 HashTable 的内部类,损坏的方法驻留在其中--
public class Entry<Key, Value> {
private Key k;
private Value v;
public Entry(Key key, Value value) {
k = key;
v = value;
}
/**
* Returns a key of pair;
*/
public Key getKey() {
return this.k;
}
/**
* Returns value of pair;
*/
public Value getValue() {
return this.v;
}
}
编辑+
所以我才意识到
public class LinkedList<Element> extends AbstractList<Element>
和
public abstract class AbstractList<Element> implements List<Element>
和
public interface List<Element> extends java.lang.Iterable<Element>
但是 LinkedList 内部有
private class Iterator extends AbstractIterator<Element>
implements nhUtilities.containers2.Iterator<Element>
也许这会导致冲突(因为我使用的 LinkedList 不是 java API LinkedList)?
最佳答案
List
是不是 Iterator
。 List
有 Iterator
。 List implements Iterable
.
List<String> list = new LinkedList<String>();
Iterator<String> test = list.iterator();
//^^ here - you call iterator()
您需要调用iterator()
在List
上获得 Iterator
实例。
所以你的方法应该阅读
public Iterator<Entry> iterator(){
List<Entry> iter = new LinkedList<Entry>();
return iter.iterator();
}
请务必调用 iterator()
关于List
.
确保您的类(class) implements Iterable<Entry>
,作为 Iterable
上的泛型类型参数接口(interface)在 interface
中得到回应方法。
编辑
从OP发布的类(class)来看,Entry
之间似乎存在混淆。在你的interface
和Entry
在iterator()
方法。
您的Entry
类在 <K,V>
中是通用的而且你的方法根本不通用。
所以你的界面要求你的 iterator
方法返回原始 Entry
或Entry<?,?>
来自iterator
方法。
大概你的实现类实现了 Entry<K,V>
并返回 Iterator<Entry<K,V>>
在该方法的实现中。
这与您的 interface
中的契约(Contract)不兼容.
您的interface
方法应该是:
Iterator<Entry<Key,Value>> iterator();
更一般地说:
interface
应该延长Iterable<Entry<K,V>>
这允许您使用增强的 for-each-loop,而不是添加您自己的自定义方法。HashTableInterface<K,V>
public
在你的interface
-interface
方法是public
默认情况下,这只会增加噪音。所以,你的interface
需要分成两部分,最终得到:
public interface Entry<K, V> {
K getKey();
V getValue();
void setValue(V value);
}
public interface HashTableInterface<K, V> extends Iterable<Entry<K, V>> {
boolean isEmpty();
V get(K key);
void add(K key, V value);
void remove(K key);
void clear();
}
编辑 MK2
一个List<Entry<K, V>>
与 List<TableEntry<K, V>>
不一样 .
正如我一开始所怀疑的那样,你(令人困惑地)有两个 Entry
类,其中一个嵌套在 interface
中以及一个嵌套在实现中的内容。
所以在你的情况下List<Entry<K, V>>
与 List<Entry<K, V>>
不同。看到困惑来自哪里了吗?
您的List
是实现的Entry
和你的interface
需要Entry
interface
的类型。
同样,你的Iterator<Entry..
该方法返回的是错误的 Entry
类型。因此,编译器提示您的实现没有实现您的 Interface
.
这是一个有效的示例;
class HashTable<K, V> implements HashTableInterface<K, V> {
private class TableEntry<K, V> implements Entry<K, V> {
}
private final List<Entry<K, V>> entries = new LinkedList<>();
@Override
public Iterator<Entry<K, V>> iterator() {
return entries.iterator();
}
}
请注意,如果我更改 List
至List<TableEntry<K, V>>
该代码将无法编译。
这里的要点是永远不要将类称为同一事物,即使这看起来确实是个好主意。
关于Java、泛型和迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15989075/
如果您有超过 1 个具有相同类名的(动态)文本框,并使用 jquery 循环遍历每个所述文本框,您是否可以假设每次选择文本框的顺序都是相同的? 示例: 文本框 1 值 = 1文本框 2 值 = 2文本
有人知道为什么这段代码无法顺利运行吗?它似乎不喜欢使用yield关键字进行迭代:我正在尝试从任何级别的列表或字典中挖掘所有数字(对列表特别感兴趣)。在第二次迭代中,它找到 [2,3] 但无法依次打印
我关于从 mysql 数据库导出数据并将其保存到 Excel 文件(多表)的创建脚本。我需要让细胞动态基因化。该脚本正确地显示了标题,但数据集为空。当我“回显”$value 变量时,我检查了数据是否存
我正在尝试在 Python 中运行模拟,由此我绘制了一个数组的随机游走图,给定了两个变量参数的设定水平。 但是,我遇到了一个问题,我不确定如何迭代以便生成 250 个不同的随机数以插入公式。例如我已经
我是学习 jquery 的新手,所以如果这是一个相对简单的问题,我深表歉意。我有一个 ID 为 ChartstoDisplay 的 asp.net 复选框列表。我正在尝试创建 jquery 来根据是否
我正在尝试根据在任意数量的部分中所做的选择找出生成有效案例列表的最佳方法。也许它不是真正的算法,而只是关于如何有效迭代的建议,但对我来说这似乎是一个算法问题。如果我错了,请纠正我。实现实际上是在 Ja
如果我使用 sr1 为 www.google.com 发送 DNSQR,我会收到几个 DNSRR(s) 作为回复,例如(使用 ans[DNSRR].show() 完成): ###[ DNS Resou
假设有这样一个实体类 @Entity public class User { ... public Collection followers; ... } 假设用户有成千上万的用户关注者。我想分页..
这个问题已经有答案了: 已关闭11 年前。 Possible Duplicate: Nested jQuery.each() - continue/break 这是我的代码: var steps =
我刚从 F# 开始,我想遍历字典,获取键和值。 所以在 C# 中,我会说: IDictionary resultSet = test.GetResults; foreach (DictionaryEn
我知道已经有很多关于如何迭代 ifstream 的答案,但没有一个真正帮助我找到解决方案。 我的问题是:我有一个包含多行数据的txt文件。 txt 文件的第一行告诉我其余数据是如何组成的。例如这是我的
我有 12 个情态动词。我想将每个模态的 .modal__content 高度与 viewport 高度 进行比较,并且如果特定模态 .modal__content 高度 vh addClass("c
在此JSFiddle (问题代码被注释掉)第一次单击空单元格会在隐藏输入中设置一个值,并将单元格的背景颜色设置为绿色。单击第二个空表格单元格会设置另一个隐藏输入的值,并将第二个单元格的背景颜色更改为红
这是一个非常具体的问题,我似乎找不到任何特别有帮助的内容。我有一个单链表(不是一个实现的链表,这是我能找到的全部),其中节点存储一个 Student 对象。每个 Student 对象都有变量,尽管我在
有没有办法迭代 IHTMLElementCollection? 比如 var e : IHTMLLinkElement; elementCollection:IHTMLElementCollect
我正在尝试用 Java 取得高分。基本上我想要一个 HashMap 来保存 double 值(因此索引从最高的 double 值开始,这样我更容易对高分进行排序),然后第二个值将是客户端对象,如下所示
我想在宏函数中运行 while/until 循环,并限制其最大迭代次数。我找到了如何在“通常”sas 中执行此操作: data dataset; do i=1 to 10 until(con
Iterator iterator = plugin.inreview.keySet().iterator(); while (iterator.hasNext()) { Player key
晚上好我有一个简单的问题,我警告你我是序言的新手。假设有三个相同大小的列表,每个列表仅包含 1、0 或 -1。我想验证对于所有 i,在三个列表的第 i 个元素中,只有一个非零。 此代码针对固定的 i
我在 scheme 中构建了一个递归函数,它将在某些输入上重复给定函数 f, n 次。 (define (recursive-repeated f n) (cond ((zero? n) iden
我是一名优秀的程序员,十分优秀!