private ArrayList<Doctor> doctors = new ArrayList<Doctor>();
doctors.add(new Doctor());
doctors.add(new Doctor());
doctors.add(new Doctor());
doctors.add(new Surgeon());
doctors.add(new Surgeon());
doctors.add(new Surgeon());
for (Doctor doctor: doctors) {
if (doctor.getAssignedPatient() != null) {
if (doctor.aDayPasses()) {
System.out.println("A " + convertSpecialism(doctor.getSpecialism()) + " treated their patient.");
shortBreak();
}
} else {
break;
}
}
Works fine however when I try to do this:
for (Surgeon doctor: doctors) {
if (doctor.getAssignedPatient() != null) {
if (doctor.aDayPasses()) {
System.out.println("A " + convertSpecialism(doctor.getSpecialism()) + " treated their patient.");
shortBreak();
}
} else {
break;
}
}
There is a syntax error, how do I for loop through the surgeons I have added to the ArrayList of type doctor.
Assume that Surgeon extends doctor.
user2177940, you can't do what you're trying to do in the second snippet. What's wrong with the code in the first snippet?