|
-
Feb 24th, 2003, 11:43 PM
#1
Thread Starter
Addicted Member
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
-
Feb 25th, 2003, 03:15 PM
#2
-
Feb 25th, 2003, 06:34 PM
#3
PowerPoster
That is fully correct :-) However I'd recommend you using set(index, obj) to set the object to the specified position at the end of your array..
As Phenix already mentioned: the add() function really ADDS an item, therefore you'd not have to resize the array by yourself nor keep track of the index or size. If you want to resize by hand, use set.
-
Feb 25th, 2003, 11:05 PM
#4
Thread Starter
Addicted Member
Cheers you guys!
I hear ya loud and clear, I love ArrayList. I wish they had one in c++... guess I'll just have to write one! ;-)
Thanks guys!
NOMAD
-
Feb 27th, 2003, 07:16 AM
#5
Actually the standard C++ vector is exactly the same as ArrayList. ArrayList is the same as the Java Vector, except for a few interface changes - but it has the same functionality and storage method.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Feb 27th, 2003, 08:23 AM
#6
PowerPoster
Well there's exactly one difference: Vectors are synchronized while ArrayLists aren't. This however only makes a difference when working with threads.
-
Feb 27th, 2003, 11:53 AM
#7
Yep, that's right. Easily amended using a synchronizing wrapper.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|