ArrayList BAD! MONGO SMASH!
I'm using an ArrayList in a Minimum Binaray Heap Priority Queue. I'm kinda using it like a Vector. I'm declaring it new ArrayList() and implimenting this code:
Code:
int newNode = (_queue.size() + 1);
_queue.ensureCapacity( newNode );
_queue.add(newNode, node);
I get a runtime error saying that I'm out of bounds... just from that snippet alone I didn't think I could go out of bounds.
The error says:
at java.util.ArrayList.add(ArrayList.java:367)
at MinBinaryHeapPriorityQueue.enqueue
(MinBinaryHeapPriorityQueue.java:82)
Line 82 is .add obviously, it says:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
Whats up with this? Is it something funny about ensureCapacity? I've never used it before. Sun's site doesn't say anything about take capacity -1 or anything... I've tried declaring the ArrayList( 100 ) no good. I've tried _queue.ensureCapacity( newNode + 1 ); and +2 and *2 still nothing.
Anything is a help as I'm on a tight deadline for this project. Thanks!
NOMAD
Grr Grr Fire Box Bring Pain Grr
It looks like no matter what size you personally make it, you are trying to add (or rather INSERT) at the next largest (and NON-EXISTENT) integer, hence the error.
add(int index, Object element)
Inserts the specified element at the specified position in this list.
IndexOutOfBoundsException - if index is out of range (index < 0 || index > size()).
So if size()==10, it would not have an element # 10. It would be elements 0 to 9.
Try
add(Object o)
Appends the specified element to the end of this list.
And see if that works for you. This adds to the end of the list and seems to be what you want anyway. Otherwise, watch out for the index you are trying to use.
Grr Grr, Let's make it 1000 elements and add to location 1002. Grr
I feel your pain buddy! BTW, your subject title had me rolling on the floor. Now I have a pain in my side. ;)