为带有节点的java哈希表编写自己的迭代器

为带有节点的java哈希表编写自己的迭代器,java,iterator,nodes,hashtable,Java,Iterator,Nodes,Hashtable,对于我们的java类,我们希望在我们的SymbolTable类(本质上只是一个哈希表)中实现我们自己的迭代器。我已经完成了SymbolTable类,但没有完成迭代器类。我不知道如何构建构造函数,以及如何正确填写hasNext();方法。 以下是内部节点类以供参考: private class Node { // nodes used to build the linked lists used in a // separate chaining hash table pr

对于我们的java类,我们希望在我们的SymbolTable类(本质上只是一个哈希表)中实现我们自己的迭代器。我已经完成了SymbolTable类,但没有完成迭代器类。我不知道如何构建构造函数,以及如何正确填写hasNext();方法。 以下是内部节点类以供参考:

private class Node {
    // nodes used to build the linked lists used in a
    // separate chaining hash table
    private String key;
    private Object data;
    private Node next;

    private Node(String k, Object d, Node n) {
        key = k;
        data = d;
        next = n;
    }
}

private Node table[];
private int tableSize;
//Used in iterator
现在,这里是我到目前为止对迭代器的介绍:

public class STIterator implements Iterator<String> {
    // An iterator that iterates through the keys in the table
    // each call to next returns the next key in the table
    Node head;
    Node current;
    int pos = 0;

    public STIterator() {
        current = table[0];

    }

    public boolean hasNext() {
        return current != null;
    }

    public String next() {
        // have to move down to location i+1 usually
        // move to next element in table thats not null
        // PRE: hasNext()
        if (hasNext()) {
            String data = (String) current.data;
            current = current.next;
            return data;
        }
        return null;
    }

}
公共类缝合器实现迭代器{
//遍历表中键的迭代器
//每次调用next都会返回表中的下一个键
节点头;
节点电流;
int pos=0;
公共缝纫机(){
电流=表[0];
}
公共布尔hasNext(){
返回电流!=null;
}
公共字符串next(){
//通常必须下移到位置i+1
//移动到表中不为null的下一个元素
//PRE:hasNext()
if(hasNext()){
字符串数据=(字符串)current.data;
当前=当前。下一步;
返回数据;
}
返回null;
}
}

所以本质上,这个迭代器扫描一个包含特定单词的哈希表(从一本书中扫描)。因此,我们希望迭代器在表中循环,直到找到不为null的数据(对象d)。表示哈希表中包含一个单词的特定“slot”(在node类中由key给出)/

我想目前您的代码如下所示:

public class SymbolTable {
...
   private class Node {
   ...
   }
   public class STIterator implements Iterator<String> {
   ...
   }
}

现在您有了一些可运行的代码,您可以询问有关它的问题。请尝试使用新代码编辑您的问题,以及您的程序出现的任何编译错误、异常或不希望出现的行为。

这是一个真正棘手的问题,我不知道有什么更好的方法来真正回答它,而不是给您答案。我假设您不需要支持
remove()
;如果你这样做了,我就把那部分留作练习

// FYI: this class doesn't have to be, and arguably shouldn't be, public
public class STIterator implements Iterator<String> {
    // An iterator that iterates through the keys in the table
    // each call to next returns the next key in the table
    Node current = null;
    int pos = 0;

    public STIterator() {}

    public boolean hasNext() {
      while (current == null && pos < table.length) {
        current = table[pos++];
      }
      return current != null;
    }

    public String next() {
      if (!hasNext()) throw new NoSuchElementException();
      String k = current.key;
      current = current.next;
      return k;
    }
}
//仅供参考:这个类不一定是公共的,也不应该是公共的
公共类缝合器实现迭代器{
//遍历表中键的迭代器
//每次调用next都会返回表中的下一个键
节点电流=零;
int pos=0;
公共缝合器(){}
公共布尔hasNext(){
while(当前==null&&pos

这是基于您试图迭代整个表的假设。如果要查找特定的键,可能根本不应该使用迭代器,而应该使用循环。

您的问题是什么,即迭代器在做(或不做)什么?关于构造器,您有什么不确定的地方?是的,对不起,是SO的新成员。我需要迭代器遍历哈希表中的字符串键。我知道我从一开始就启动了迭代器(因此从表[0]开始),但我不确定如何编写构造函数来正确构建迭代器,以及如何使用next和hasNext方法来手动迭代(这意味着不使用java提供的utils)。假设您有一个哈希表,其中tableSize为2,table[0]和table[1]每个节点都有一个next为null的节点。迭代器是否依次返回这两个节点中的每一个?它应该调用next,直到在该特定数据中找到一个元素。因此,它应该循环,直到找到一个值(这是我们的字符串键),因此,如果没有找到,它应该退出迭代。我让它简单地返回null。您只在第一个bucket上的列表中进行迭代。一旦从当前bucket到达列表的末尾,您还需要迭代bucket
// FYI: this class doesn't have to be, and arguably shouldn't be, public
public class STIterator implements Iterator<String> {
    // An iterator that iterates through the keys in the table
    // each call to next returns the next key in the table
    Node current = null;
    int pos = 0;

    public STIterator() {}

    public boolean hasNext() {
      while (current == null && pos < table.length) {
        current = table[pos++];
      }
      return current != null;
    }

    public String next() {
      if (!hasNext()) throw new NoSuchElementException();
      String k = current.key;
      current = current.next;
      return k;
    }
}