Prevent IndexOutOFBoundEx:
In genCivilVehicles(), the casting might step over the number of cars size of the array. So, use try catch blocks and skip in that case.
This commit is contained in:
parent
9fb653397f
commit
f4bb5e6099
@ -37,7 +37,7 @@ public class MakkahCity {
|
|||||||
timeManager.step(Calendar.MINUTE, 1);
|
timeManager.step(Calendar.MINUTE, 1);
|
||||||
System.out.println(timeManager.getCurrentTime());
|
System.out.println(timeManager.getCurrentTime());
|
||||||
//TODO: add civil cars in loop iterations. (noise)
|
//TODO: add civil cars in loop iterations. (noise)
|
||||||
|
//noise based on time of day (From PDate)
|
||||||
//TODO: Move busses and vehicles.
|
//TODO: Move busses and vehicles.
|
||||||
|
|
||||||
//TODO: Update streets.
|
//TODO: Update streets.
|
||||||
@ -153,16 +153,23 @@ public class MakkahCity {
|
|||||||
* @return Array of 70% Sedans and 30% SUVs
|
* @return Array of 70% Sedans and 30% SUVs
|
||||||
*/
|
*/
|
||||||
private static CivilVehicle[] generateCivilVehicles(int numberOfCars) {
|
private static CivilVehicle[] generateCivilVehicles(int numberOfCars) {
|
||||||
|
|
||||||
CivilVehicle[] cars = new CivilVehicle[numberOfCars];
|
CivilVehicle[] cars = new CivilVehicle[numberOfCars];
|
||||||
int sedans = (int)(numberOfCars*0.7);
|
int sedans = (int)(numberOfCars*0.7);
|
||||||
int SUVs = numberOfCars - sedans;
|
int SUVs = numberOfCars - sedans;
|
||||||
|
|
||||||
for (int i = 0; i < sedans; i++) {
|
for (int i = 0; i < sedans; i++) {
|
||||||
cars[i] = new Sedan(getRandom(25,32)/10);
|
cars[i] = new Sedan(getRandom(25,32)/10);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < SUVs; i++) {
|
for (int i = 0; i < SUVs; i++) {
|
||||||
|
try {
|
||||||
cars[sedans+i] = new SUV(getRandom(35,45)/10);
|
cars[sedans+i] = new SUV(getRandom(35,45)/10);
|
||||||
}
|
}
|
||||||
|
catch (IndexOutOfBoundsException ex) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
return cars;
|
return cars;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user