填充LinkedList的数组列表



我一直在做这个项目,它创建了一个由带有桶的散列结构组成的一致性数据结构。每次尝试添加新元素时,我都会遇到一个问题。我得到一个IndexOutOfBoundsExeption我知道,当我创建一个一定大小的数组列表时,它只是保留内存,但实际上并不包含任何元素。我如何填充这个数组列表,以便我可以在其中添加元素。

public ConcordanceDataStructure(int i) {
            this.size=fourKPrime(i);
            this.hashtable=new ArrayList<LinkedList<ConcordanceDataElement>(size);

        }

这个方法出现问题:

public void add(String term, int lineNum){ 
        boolean noError;
        boolean hit = false;
        int pass, q, offset, ip;
        int pk = Math.abs(term.toLowerCase().hashCode()); // preprocess the key
        if (nodes<=getTableSize())// insert the node
        { 
            pass = 0;
            q = pk / getTableSize();
            offset = q;
            ip = pk % getTableSize();
            if(q%getTableSize() == 0)
                offset = 9967;
        }
        else
        {
            System.out.println("FULL");
            return;
        }
        while(pass < getTableSize())
        { 
            if(hashtable.get(ip) == null){  //PROBLEM IS HERE
                hit = true;
                break;
            }
            ip = (ip + offset)%getTableSize();
            pass = pass +1;
        }
        if(hit == true) // insert the node
        { 
            hashtable.add(ip, new LinkedList<ConcordanceDataElement>());
            hashtable.get(ip).add(new ConcordanceDataElement(term));
            hashtable.get(ip).get(hashtable.get(ip).size()).addPage(lineNum);
            nodes++;
        }

使用LinkedList数组代替ArrayList<LinkedList>:

[编辑以避免编译错误]

protected LinkedList<ConcordanceDataElement>[] hashtable;

public ConcordanceDataStructure(int i) {
   this.size=fourKPrime(i);
   @SuppressWarnings("unchecked")
   this.hashtable=new LinkedList[this.size];

}
protected int getTableSize() {
  return this.hashtable.length;
}
public void add(String term, int lineNum){ 
    boolean noError;
    boolean hit = false;
    int pass, q, offset, ip;
    int pk = Math.abs(term.toLowerCase().hashCode()); // preprocess the key
    if (nodes<=getTableSize())// insert the node
    { 
        pass = 0;
        q = pk / getTableSize();
        offset = q;
        ip = pk % getTableSize();
        if(q%getTableSize() == 0)
            offset = 9967;
    }
    else
    {
        System.out.println("FULL");
        return;
    }
    while(pass < getTableSize())
    { 
        if(hashtable[ip] == null){  // no more problems
            hit = true;
            break;
        }
        ip = (ip + offset)%getTableSize();
        pass = pass +1;
    }

相关内容

  • 没有找到相关文章

最新更新