Problems adding elements to a vector-VBForums
Results 1 to 6 of 6

Thread: Problems adding elements to a vector

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2011
    Posts
    57

    Problems adding elements to a vector

    Well, I have a vector called "Vector" and I'm adding vehicles to this vector with the "insertElementAt" statement.

    I do:
    Vector <vehicle> saveVehicles= new Vector();

    if(option1){
    Cars Car1 = new Car(...);
    Vector.insertElementAt(Car1, 0);}
    if(option2){
    Cars Car2 = new Car(...);
    Vector.insertElementAt(Car2, 1);}
    if(option3){
    Cars Car3 = new Car(...);
    Vector.insertElementAt(Car3, 2);}
    if(option4){
    Cars Car4 = new Car(...);
    Vector.insertElementAt(Car4, 3);}

    So I add 4 cars depending of the option.
    If I create first vehicle 1, following vehicle 2, vehicle 3 and vehicle 4 in this order, is all right. The problem comes when I'm trying to create first any vehicle that is not the vehicle 1.
    If I try to create vehicle 3 first, I get this error:

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 > 0
    at java.util.Vector.insertElementAt(Unknown Source)
    at java.util.Vector.add(Unknown Source)
    at Simulador.main(Simulator.java:70)


    What should I do to add vehicles wherever I want of the Vector not in order??

    Thanks !

  2. #2
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Problems adding elements to a vector

    Java is not my strong side, but it seems like you want to add each "Car" at the end of the vector? Could you not just replace the insertElementAt call with a call to add?
    Code:
    if(option1){
    Cars Car1 = new Car(...);
        Vector.add(Car1);
    }
    if(option2){
    Cars Car2 = new Car(...);
        Vector.add(Car2);
    }
    if(option3){
    Cars Car3 = new Car(...);
        Vector.add(Car3);
    }
    if(option4){
    Cars Car4 = new Car(...);
        Vector.add(Car4);
    }
    Edit: Besides, should you not be calling insertElementAt (or add!) on your saveVehicles instance?
    ---My Flickr photo (mostly screenshots these days!) stream. Have a look!

    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    TCP client/server connection | Retrieving the EventHandler for any Event by code.
    Me on.. BitBucket, Google Code, Github (pretty empty)

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2011
    Posts
    57

    Re: Problems adding elements to a vector

    Quote Originally Posted by Atheist View Post
    Java is not my strong side, but it seems like you want to add each "Car" at the end of the vector? Could you not just replace the insertElementAt call with a call to add?
    Code:
    if(option1){
    Cars Car1 = new Car(...);
        Vector.add(Car1);
    }
    if(option2){
    Cars Car2 = new Car(...);
        Vector.add(Car2);
    }
    if(option3){
    Cars Car3 = new Car(...);
        Vector.add(Car3);
    }
    if(option4){
    Cars Car4 = new Car(...);
        Vector.add(Car4);
    }
    Edit: Besides, should you not be calling insertElementAt (or add!) on your saveVehicles instance?
    Thanks for your reply!

    But nope, dont' want to add to the end.
    Just want to, if I need to add just the second car, add the second car to the second position of the array, and leave the positions 1, 3 and 4 empty to access to the position 2 when I want to get the information of the car.

    THanks!

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Problems adding elements to a vector

    Ah. It is not possible to add something to index X unless there are X items in the vector.
    So if you want to add something to index 3, and there are no other items in the vector, you have no other choice than to add something (possibly null) to index 0, 1 and 2.

    However, it sounds like you have a set number of elements, so why not use an array?
    Code:
    Car[] saveVehicles = new Car[4];
    if(option1){
    Cars Car1 = new Car(...);
        saveVehicles[0] = Car1;
    }
    if(option2){
    Cars Car2 = new Car(...);
        saveVehicles[1] = Car2;
    }
    if(option3){
    Cars Car3 = new Car(...);
        saveVehicles[2] = Car3;
    }
    if(option4){
    Cars Car4 = new Car(...);
        saveVehicles[3] = Car4;
    }
    Edit: I said vector instead of array in my last sentence, how silly of me.
    Last edited by Atheist; Feb 10th, 2012 at 08:47 AM.
    ---My Flickr photo (mostly screenshots these days!) stream. Have a look!

    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    TCP client/server connection | Retrieving the EventHandler for any Event by code.
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2011
    Posts
    57

    Re: Problems adding elements to a vector

    Oh, really ?? Okay then ...

    So, I have to fill with nulls all ??? Okay understood ! And how should I do??

    vectorname.insertElementAt(null, 0); ??

    THanks

  6. #6

    Thread Starter
    Member
    Join Date
    Oct 2011
    Posts
    57

    Re: Problems adding elements to a vector

    Tried it and it works.
    Thanks a lot mate, You helped me a lot.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.