How to iterate over objects in a class?
Hello,
If I create a class, let's say it is called 'Vehicle'. Vehicle has some fields in it, varOne, varTwo, varThree.
Code:
public class Vehicle {
public float varOne;
public float varTwo;
public float varThree;
public Vehicle (float one, float two, float three) {
varOne = one;
varTwo = two;
varThree = three;
}
}
If I then create 10 instances of Vehicle,
Code:
vehicleOne = new Vehicle(1,2,3);
...
vehicleTen = new Vehicle(28,29,30);
How do I then iterate over all ten Vehicles doing things with all their variables? ( eg. vehicleSix.varTwo )
My initial thought was using a for each loop but in my google'ing I found lots of people itterating over arrays with iterator().
What is the best way to do this (I haven't yet been able to even get a working test example, I'm so lost lol).
Thanks for any help.