哈希设置打印问题

时间:2014-02-28 11:11:41

标签: java iteration hashset

我们必须设计自己的HashSet类。 (不使用JAVA API)。

我实现了它,但是打印时遇到了问题。

我认为迭代构造函数存在问题。有什么建议吗?

哈希集:

package CountWords;

import java.util.Iterator;

public class HashWordSet implements WordSet  {

    private int size = 0;                    
    private Node[] buckets = new Node[8];    

    public class Node { 
    Word value;
    Node next = null;

    public Node (Word w) { value = w; }
    }

    private class HashWordIterator implements Iterator<Word> {

        private int pos, index = 0;
        private Word[] words = new Word[size];
        private Node node;

        public HashWordIterator()
        {
            for(int i=0; i<words.length;i++){
            words[index++]=node.value;
            node = node.next;
            }
        }

        public boolean hasNext() {
            if (size > pos)//root != null && root.next != null)
                return true;

            return false;
        }

        public Word next() {
            return words[pos++];
        }

        @Override
        public void remove() {
            // TODO Auto-generated method stub
        }
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public void add(Word word) {
        int pos = (word.hashCode() % buckets.length);        
        Node node = buckets[pos];

        while (node != null) {
            if (node.value.equals(word))
                return;
            else
            node = node.next;
        }

        node = new Node(word);
        node.next = buckets[pos];
        size++;    
        if (size == buckets.length)
            rehash();
        }    
    }

    private void rehash() {
        Node[] tmp = buckets;
        buckets = new Node[2*tmp.length];                    
        size = 0;        

        for (Node n : tmp) {
            if (n == null) continue;
            while (n != null) {
                add(n.value);
                n = n.next;
            }
        }
    }

    @Override
    public boolean contains(Word word) {
        int pos = (word.hashCode() % buckets.length); //
        Node n = buckets[pos];

        while (n != null) {
            if (n.value.equals(word))
                return true;
            else
                n = n.next; // stega fram i listan
        }
        return false;
    }

    public String toString () {
        StringBuffer sb = new StringBuffer();

        for (int i = 0; i < buckets.length; i++) {
            sb.append(buckets[i].value.toString());
        }

        return sb.toString();
    }

    @Override
    public Iterator<Word> iterator() {
        return new HashWordIterator() ;
    } 
}

主要

package CountWords;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Scanner;


public class WordCount2Main {

    public static void main(String[] args) {
        File file = new File("/Users/sa/Documents/workspace/1DV007/src/CountWords/Words.txt");

        HashWordSet  Hset = new HashWordSet ();
        //TreeSet<Object> Tset = new TreeSet<Object>();
        TreeWordSet Tset = new TreeWordSet();
        Scanner Scan;
        int i=0;
        try {
            Scan = new Scanner(file);

            while (Scan.hasNext()) {
                String text = Scan.next();
                Word Y=new Word(text);
                Hset.add(Y);
                i++;
            }

            System.out.println("TreeSet: " + "\n");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        System.out.println();
        Iterator<Word> iter2 = Hset.iterator();

        while(iter2.hasNext()) {
            Object o2 = iter2.next();
            System.out.print(o2 + " ");
        }

         System.out.println(Hset.size() + " Hash set:: "+Hset );
    }
}

1 个答案:

答案 0 :(得分:0)

在你的构造函数中,我看到了

words[index++]=node.value

您似乎无法在任何地方node实例化