gpt4 book ai didi

Java、泛型和迭代器

转载 作者:行者123 更新时间:2023-12-01 07:32:46 25 4
gpt4 key购买 nike

我试图理解为什么会出现此错误:

-- 这是错误--

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不是 IteratorList IteratorList 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之间似乎存在混淆。在你的interfaceEntryiterator()方法。

您的Entry类在 <K,V> 中是通用的而且你的方法根本不通用。

所以你的界面要求你的 iterator方法返回原始 EntryEntry<?,?>来自iterator方法。

大概你的实现类实现了 Entry<K,V>并返回 Iterator<Entry<K,V>>在该方法的实现中。

这与您的 interface 中的契约(Contract)不兼容.

您的interface方法应该是:

Iterator<Entry<Key,Value>> iterator();

更一般地说:

  1. 您的interface应该延长Iterable<Entry<K,V>>这允许您使用增强的 for-each-loop,而不是添加您自己的自定义方法。
  2. 根据样式约定,我们使用单个字母表示泛型类型参数,因此 HashTableInterface<K,V>
  3. 请勿使用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();
}
}

请注意,如果我更改 ListList<TableEntry<K, V>>该代码将无法编译。

这里的要点是永远不要将类称为同一事物,即使这看起来确实是个好主意。

关于Java、泛型和迭代器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15989075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com