Results 1 to 7 of 7

Thread: ArrayList BAD! MONGO SMASH!

  1. #1

    Thread Starter
    Addicted Member NOMADMAN's Avatar
    Join Date
    Aug 2002
    Location
    Closer than you think
    Posts
    237

    Angry 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

  2. #2
    Addicted Member Phenix's Avatar
    Join Date
    Sep 2002
    Location
    Near A Cube
    Posts
    228

    Wink 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.

  3. #3
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    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.

  4. #4

    Thread Starter
    Addicted Member NOMADMAN's Avatar
    Join Date
    Aug 2002
    Location
    Closer than you think
    Posts
    237
    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

  5. #5
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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.

  6. #6
    PowerPoster Fox's Avatar
    Join Date
    Jan 2000
    Location
    *afk*
    Posts
    2,088
    Well there's exactly one difference: Vectors are synchronized while ArrayLists aren't. This however only makes a difference when working with threads.

  7. #7
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    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
  •  



Click Here to Expand Forum to Full Width