Edit. thank you.
I have an array of 'normal' vehicles and 'large' vehicles. I have an assignment requiring me to divide them up to contribute to a far larger app. One array for the large vehicles, one for the normal vehicles containing all the info for each element. ArrayLists are not permitted as my instructor is teaching us fundamentals.
Sample of the array
27723 4/09/61 large 7337
28507 22-02-1983 large 7055
28558 1/05/70 normal 3518
//On button press
//recieve single item from array from main and test it
//array in main will be looped for all elements.
public String loadVehicle(Vehicle v) {
String res = Constants.OK;
boolean normBool = false;
boolean largeBool = false;
//if both arrays are full , stop the method call in the main form
if (normBool && largeBool){return Constants.ERROR;}
//if vehicle size is normal, fill the normal veh array
if(v.getSize().equals(Constants.NORMAL_SIZE))
{
for(int i = 0; i<normalVehicles.length; i++)
{
//if norm veh array element is null, add the appropriate value to it
if(normalVehicles[i] == null){normalVehicles[i] = v;}
else{normBool = true;}
}
}
//if veh size is large put it in the large veh array
else if(v.getSize().equals(Constants.LARGE_SIZE))
{
for(int iL = 0; iL<largeVehicles.length; iL++)
{
if(largeVehicles[iL] == null){largeVehicles[iL] = v;}
else{largeBool = true;}
}
}
return res;
}//end method
Seems you cannot use builtin LinkedList class too, then do this:
Add the following code in your Vehicle class:
class Vehicle(){ //YOUR OTHER PIECES OF CODES ... private static Vehicle firstLargeVehicle; private Vehicle nextLargeVehicle; private int index; public void setIndex(int index){ this.index = index; if(index == 0) Vehicle.firstLargeVehicle = this; } public int getIndex(){ return index; } public void setNextLargeVehicle(Vehicle nextLargeVehicle){ this.nextLargeVehicle = nextLargeVehicle; } public Vehicle getNextLargeVehicle(){ return nextLargeVehicle; } public addLargeVehicle(Vehicle newVehicle){ this.nextLargeVehicle = newVehicle; newVehicle.setIndex(index + 1); } public getListSize(){ Vehicle lastOne = this; while (lastOne.getNextLargeVehicle() != null){ lastOne = lastOne.getNextLargeVehicle(); } return lastOne.getIndex() + 1; } public static Vehicle[] largeVehiclesToArray(){ Vehicle[] result = new Vehicle[firstLargeVehicle.getListSize()](); Vehicle pointer = firstLargeVehicle; for (int counter = 0; pointer != null; counter ++){ result[counter] = pointer; pointer = pointer.getNextLargeVehicle(); } return result; } }
And in your main loop, do something like the following code:
Vehicle vehicle = null; for(Vehicle newVehicle : allVehicles) { if (newVehicle.isLarge()){ if (vehicle == null) { vehicle = newVehicle; vehicle.setIndex(0); }else{ vehicle.addLargeVehicle(newVehicle)); } } } Vehicle[] largeVehicles = Vehicle.largeVehiclesToArray();
And the same story goes for normal vehicles. Any question ?