2014-02-28 52 views
1

我们必须设计我们自己的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); 
    } 
} 
+5

'我在打印时遇到了问题。“您能详细说明吗?你得到错误的输出或什么? – PakkuDon

+0

我得到这个错误0 线程“main”中的异常java.lang.NullPointerException \t at CountWords.HashWordSet $ HashWordIterator。 (HashWordSet.java:36) \t在CountWords.HashWordSet.iterator(HashWordSet.java:134) \t在CountWords.WordCount2Main.main(WordCount2Main.java:49) – Jason

+0

@ user2453286:包括在你发布了同样的错误堆栈。 –

回答

0

在构造函数中我看到

words[index++]=node.value 

你似乎没有实例node随时随地

+0

现在修复了:) – Jason