【问题标题】:Populating ArrayList of LinkedList [duplicate]填充 LinkedList 的 ArrayList [重复]
【发布时间】:2016-10-19 21:47:40
【问题描述】:

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

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++;

        }

【问题讨论】:

  • 我想你想使用数组并手动调整它的大小,以便添加一个尚未初始化的位置。换句话说,编写你自己的 ArrayList。其他选项是创建一个循环大小并添加 null 元素
  • 我试过循环添加null,没用......
  • 我不明白:为什么你需要一个ArrayList 作为你的遗愿清单,而一个简单的LinkedList 数组就可以了?这不像您要在每次插入时调整它的大小。另外,您为什么要避免使用HashMap 类并重新开始发明轮子?
  • 我猜所以我们理解了散列结构的概念。
  • 我会尝试做一个链表数组

标签: java arraylist linked-list indexoutofboundsexception


【解决方案1】:

使用LinkedList 的数组代替ArrayList&lt;LinkedList&gt;

[编辑以避免编译错误]

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;
    }

【讨论】:

  • 无法创建 LinkedList 的通用数组 ConcordanceDataStructure.java
  • 我认为数组的大小必须事先确定。
  • @IgorKamalov “我认为必须事先确定数组的大小。”不,不是这样。我forgot about it。现在更正代码。
  • @IgorKamalov 已更正
  • 非常感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-24
  • 1970-01-01
  • 2014-09-17
  • 2014-12-31
  • 2021-12-19
  • 1970-01-01
  • 2012-07-24
相关资源
最近更新 更多