-
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 !
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
|