So I'm using an ArrayList (to create a hash table structure) like so:

protected ArrayList bucketArray;

Later in the constructor, it initializes it as:

bucketArray = new ArrayList(500);

When I use it and try to insert a Node from a linked list in like this:

bucketArray.add(357, nodeToBeInserted);

it gives me the error:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 347, Size: 0

Shouldn't the size be set as 500 such that I can insert at any given location? If I change it to just add without a location, it'll append to the ArrayList perfectly fine but not in the order I need for a hash table obviously. Any suggestions in what I can do to resolve this?

Thanks for the help!

No. That 500 sets the initial size of the array that "backs" the arraylist, as well as the size of all subsequent arrays used to back the arraylist once it exceeds that size, but the "indexes" are populated from front to back and are not "immediately" available. If the arraylist has already grown to that size, then you can insert into an arraylist in that manner, but you cannot set an index beyond the end of the current size of the list.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.