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 :) !
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?
Re: Problems adding elements to a vector
Quote:
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!
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.
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 :)
Re: Problems adding elements to a vector
Tried it and it works.
Thanks a lot mate, You helped me a lot.