1
import java.util.*;
import java.util.ArrayList;

class MyHashTable<K extends Comparable<K>, E> {

    private ArrayList<Entry<K,E>> bucket = new ArrayList<Entry<K,E>>();
    private int bucketSize;
    private int collisionCount = 0;

    // Constructor that takes number of buckets as input
    public MyHashTable( int len ) 
    {
        this.bucketSize = len;
        for ( int i = 0; i < len; i++ ) 
        {
            bucket.set( i, null ); //ERROR APPEARS ON THIS LINE
        }
    }

and is evoked when I call from another method :

MyHashTable<MyString, AnimalRecord> linearProbing = new MyHashTable<MyString, AnimalRecord>(59);
linearProbing.put( lion.name, lion );

6 Answers 6

4
private ArrayList<Entry<K,E>> bucket = new ArrayList<Entry<K,E>>();

Creates an empty arrayList.

The set method

Replaces the element at the specified position in this list with the specified element
Throws: IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

So

bucket.set( i, null );

tries to set null to the ith element (starting at 0). But the arraylist is empty, and that is why you get the exception.

What you want is the add method:

for ( int i = 0; i < len; i++ ) 
{
    bucket.add(null);
}

adds null to the end of the ArrayList.

Sign up to request clarification or add additional context in comments.

4 Comments

I'm not sure that's the error (mainly because I just added a size and the bug still occurred) - but can't a list grow with add - even if the size is empty?
@user3771739 A list will grow with add. It will not grow with set. You need to use add where you are using set.
Will it be 10th instead of the first? There is a default capacity in the intialization. developer.classpath.org/doc/java/util/ArrayList-source.html
@MingtaoZhang capacity is different from size. An ArrayList is backed by an array and capacity is the size of that array. Java manages the array (increase/decrease size etc) and mostly transparent. size is the actual number of elements in the ArrayList. capacity >= size.
2

You need to add not set at first sight.

Look at the doc of set() method

Throws: IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())

You met with with the condition index >= size()

Comments

1

Initially, the ArrayList is empty, when you create a new instance of it:

private ArrayList<Entry<K,E>> bucket = new ArrayList<Entry<K,E>>();

When you are calling the set method on the ArrayList, you are trying to replace an existing element, which is not found, because the list is empty.

You should use add, instead of set if adding the element to the list is what you want.

Comments

0

You ArrayList (bucket) doesn't have any elements in it - bucket.set replaces an element, it doesn't insert an element.

See http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#set(int,%20E)

Call ArrayList.add() instead as that will append an element to the end of the array.

Comments

0

Rather obvious I think, your ArrayList is created with default capacity (10), while you are trying to access indexes above it (up to 58).

Comments

0

Take a look at the Java-Documentation for the Class ArrayList and the method you are using:

The method set "Replaces the element at the specified position in this list with the specified element."

At the time of your method call you do not have an ArrayList with an Element 59. The initial size of an ArrayList constructed with ArrayList() Standard-Constructor is 10.

You could use a constructor ArrayList(int initialCapacity)

Find the documentation here:

http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.