How can we make an ArrayList in java consistent. Means when I have updated the ArrayList in one function, it is updated. But, after the exit from that function it is again setting it's size to 0.
public static ArrayList<Vehicle> al=new ArrayList<Vehicle>();
java.util.Iterator<Vehicle> itr= al.iterator();
@SuppressWarnings("unchecked")
public boolean addVehicleToSlot(Vehicle vehicle) {
if((vehicle.slotNo<=0||vehicle.slotNo>40)&&(vehicle.getVehicleType()!="Car"
||vehicle.getVehicleType()!="Truck"||vehicle.getVehicleType()!="Two Wheeler")){
new InvalidSlotException("Slot alraedy allotted");
return false;
}
int iter=0;
int len=al.size();
if(len==0){
al.add(vehicle);
return true;
}
while(iter< len){
Vehicle veh=(Vehicle)itr.next();
if(veh.getSlotNo()==vehicle.getSlotNo()){
new SlotNotFoundException("No slot allotted");
return false;
}
else{
al.add(vehicle);
for(Vehicle vehi:al){
System.out.println(al);
}
return true;
}
}
return false;
}
My function call is this:
Vehicle v1=new Vehicle(o1,"ts75","Truck",37,48);
ParkingManagement p1=new ParkingManagement();
System.out.println(p1.addVehicleToSlot(v1));
Please help me out with this.