-
Feb 10th, 2012, 06:54 AM
#1
Thread Starter
Member
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 !
-
Feb 10th, 2012, 07:01 AM
#2
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?
-
Feb 10th, 2012, 07:28 AM
#3
Thread Starter
Member
Re: Problems adding elements to a vector
Originally Posted by Atheist
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!
-
Feb 10th, 2012, 07:34 AM
#4
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.
-
Feb 10th, 2012, 08:14 AM
#5
Thread Starter
Member
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
-
Feb 10th, 2012, 08:19 AM
#6
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|