-1

I've been working on this project that creates a Concordance Data Structure which consists of Hashed structure with buckets. There is a problem I am running into every time I am trying to add a new element. I get a IndexOutOfBoundsExeption I understand that when I create an ArrayList of certain size, it just reserves the memory but does not actually contains any elements yet. How do I populate this ArrayList so i can ad elements in it.

public ConcordanceDataStructure(int i) {

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


        }

Problem occurs in this method:

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

        }

1 Answers1

0

Use an array of LinkedList instead of ArrayList<LinkedList>:

[edited to avoid the compilation error]

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;
    }
Adrian Colomitchi
  • 3,974
  • 1
  • 14
  • 23